1

当我创建一个类别时,这是代码工作正常,当我们创建一个子类别时它会出错。

在实体文件中:

  /**
     * 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;

在表单生成器中:

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'

            ));
}
4

3 回答 3

2

更改为实体文件:

   /**
     * @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();
}

更改为 formbuilder 也:

    ->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',
            ));

使用此代码。随意享受。:)

于 2012-12-12T07:00:52.960 回答
1

你可能正在做类似的事情
$category->setParent($parentId);

你应该在哪里做
$category->setParent($parent);

$parent'MusicCategory' Doctrine 类型的对象在哪里
与实体一起使用,而不是 id(s)

也看看
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

主要是关于自引用关联的部分。

于 2012-12-11T07:15:58.550 回答
0

我认为这是一个关联映射问题...您应该检查关联...

于 2012-12-11T07:10:13.160 回答