1

This is code is work fine when I am creating a category, when we create a subcategory it gives error.

In Entity file:

  /**
     * Music\Bundles\Core\Entity\MusicCategory
     * @ORM\Table(name="ms_musiccategory")
     * @ORM\Entity()
     * @ORM\Entity(repositoryClass="Music\Bundles\Core\Entity\Repository\CategoryRepository")
    * @UniqueEntity(fields={"name"},message="The name is already in this system.")
  */
    class MusicCategory 
   {
/**
     * @ORM\Column(name="id",type="integer")
     * @ORM\Id()
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

    /**
     *@ORM\ManyToOne(targetEntity="MusicCategory")
    *@ORM\JoinColumn(name="parent_Id", referencedColumnName="id")
     * 
     */
    private $parentid;

In Formbuilder:

public function buildForm(FormBuilder $builder, array $options)
{
   $builder
        ->add('name')
        ->add('description')
        ->add('parentid', 'entity', array('class'=>'MusicCoreBundle:MusicCategory',
            'property'  => 'name',
            'required'  => false,
            'query_builder' => function(EntityRepository $er) {return $er->createQueryBuilder('s')->orderBy('s.name', 'ASC');},
            'empty_value' => 'No category'

            ));
}
1
  • 1
    If I knew the answer, I would post it. But I don't. Commented Dec 11, 2012 at 8:35

3 Answers 3

2

Change into Entity file:

   /**
     * @ORM\Column(name="id",type="integer")
     * @ORM\Id()
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

    /**
      * @ORM\OneToMany(targetEntity="MusicCategory", mappedBy="parent")
     */
    protected $children;

   /**
     * @ORM\ManyToOne(targetEntity="MusicCategory", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
    */
    protected $parent;

    public function __construct()
{
         $this->parentId = null; // Default value for column parent_id
         $this->children = new \Doctrine\Common\Collections\ArrayCollection();
}

Change into formbuilder also:

    ->add('parentid', 'entity', array('class'=>'MusicCoreBundle:MusicCategory',
            'property'  => 'name',
            'required'  => false,
             'query_builder' => function(EntityRepository $er) {return $er->createQueryBuilder('s')->orderBy('s.name', 'ASC');},//->where('s.parentid is NULL')
            'empty_value' => 'No category',
            ));

Use this code.feel free to enjoy. :)

Sign up to request clarification or add additional context in comments.

Comments

1

You are probably doing something like
$category->setParent($parentId);

where you should do
$category->setParent($parent);

where $parent is an object of type 'MusicCategory'
Doctrine works with entities, not id(s)

also take a look at
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

mainly the parts about self-referencing associations.

2 Comments

thanks for replying but where should i write this code?please help me..:)
i have added code in controller: if($parent_id = $form['parentid']->getData()) { $category->setParentId($parent_id->getId()); }
0

I think its a association mapping problem... You should check the association...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.