【发布时间】:2013-03-17 10:40:32
【问题描述】:
您好,我已经完全成功地设置了我的实体 onetoMany 和 ManyToOne,我生成了 setter 和 getter,并在用户实体中创建了这个方法:
用户实体:
/**
* @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="followeeuser")
*/
protected $followees;
请求实体:
/**
* @ORM\ManyToOne(targetEntity="TB\UserBundle\Entity\User", inversedBy="followees")
* @ORM\JoinColumn(name="followee_id", referencedColumnName="id", nullable=false)
*/
protected $followeeuser;
当我使用我自己的自定义查询时,它工作得很好......但我无法弄清楚如何使用 symfony 生成的这个函数:
public function addFollowee(\TB\UserBundle\Entity\User $followee)
{
$this->followees[] = $followee;
}
我不知道该传递什么...我尝试首先根据用户的 id 从树枝获取用户对象...效果很好,但发生错误:
$user->addFollowee($userRepository->find($target_user_id));
Found entity of type TB\UserBundle\Entity\User on association TB\UserBundle\Entity\User#followees, but expecting TB\RequestsBundle\Entity\Requests
【问题讨论】:
-
这个函数是由 Symfony2 创建的?它应该被称为:
public function addFollowee(TB\RequestsBundle\Entity\Requests $followee) -
是的,即使我会传递给它什么?
-
我不知道你在那里做什么。但如果您尝试为用户设置“关注”功能,请看这里:docs.doctrine-project.org/projects/doctrine-orm/en/latest/…
-
我看到了这十亿次 :) 我无法使用它,因为我的实体中有额外的字段......我正在尝试通过像 addFollowee 这样的用户设置追随者......并且不知道我需要放什么那里...现在我这样做了 => 请求存储库,然后是 new Requests() 并填写所有字段...但我正在寻找最好的方法
标签: symfony entity many-to-one relationships