Symfony2:我有错误“spl_object_hash()期望参数1是对象,给定整数”

时间:2012-12-11 07:06:08

标签: php symfony doctrine-orm

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

在实体文件中:

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

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'

            ));
}

3 个答案:

答案 0 :(得分: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',
            ));

使用此代码。免费享受。 :)

答案 1 :(得分: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

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

答案 2 :(得分:0)

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