Doctrine2 - OneToMany / ManyToOne双向不起作用

时间:2012-09-08 09:13:15

标签: symfony doctrine-orm one-to-many fosuserbundle

大家好,感谢您的帮助,

我正在处理一个问题,而我想与Doctrine2(& Symfony2)建立OneToMany / ManyToOne双向关系。

以下是我的两个类:User(扩展FOSUser)和代表视频集合的集合。 当然,用户可以拥有多个集合,但集合只与一个用户相关。

/* COLLECTION */
namespace Com\ComBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * Com\ComBundle\Entity\Collection
 *
 * @ORM\Table(name="collection")
 * @ORM\HasLifecycleCallbacks
 */
class Collection
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Com\UserBundle\Entity\User", inversedBy="collections")
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;

    /**
     * Set user
     *
     * @param Com\UserBundle\Entity\User $user
     * @return Collection
     */
    public function setUser(\Com\UserBundle\Entity\User $user) {
        $this->user = $user;
        return $this;
    }

    /**
     * Get user
     *
     * @return Com\UserBundle\Entity\User User 
     */
    public function getUser() {
        return $this->user;
    }

}

和用户,

    /* USER */    
namespace Com\UserBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /* 
     * @ORM\OneToMany(targetEntity="Com\ComBundle\Entity\Collection", mappedBy="user")
     */
    private $collections;


    public function __construct()
    {
        parent::__construct();
        $this->collections = \Doctrine\Common\Collections\ArrayCollection();
    }       
}

当我使用doctrine:generate:entities命令时,它不会生成相对于$ collections(get / add / remove)的方法,即使我自己编写它,它也不起作用。 例如,getCollections()返回NULL。

我在这段代码中缺少什么?

0 个答案:

没有答案
相关问题