Doctrine orm 如何获得实体中的关联?
我希望这在理论上是可能的。我知道这是自动的。我想做的是: 我有两张桌子:Doctrine orm 如何获得实体中的关联?,doctrine-orm,Doctrine Orm,我希望这在理论上是可能的。我知道这是自动的。我想做的是: 我有两张桌子: workflow (id, name) inbox (id, workflow_id, name) /** * @ORM\ManyToOne(targetEntity="Workflow") * @ORM\JoinColumn(nullable=false) */ protected $workflow; 和两个实体: Workflow and Inbox 当然,在我的收件箱实体中,我有以下
workflow (id, name)
inbox (id, workflow_id, name)
/**
* @ORM\ManyToOne(targetEntity="Workflow")
* @ORM\JoinColumn(nullable=false)
*/
protected $workflow;
和两个实体:
Workflow and Inbox
当然,在我的收件箱实体中,我有以下内容(用于关联两个表):
一切都很好。但是,我希望能够从与该工作流关联的工作流实体中获取收件箱。我不知道怎么做
推进做到这一点非常简单,您可以这样做:
$workflow = WorkflowQuery::create()
->filterById(1)
->findOne(1);
$inboxes = $workflow->getInboxs()
//Propel just addes 's' to methods that return associations
如何在Doctrine2中实现类似的功能?大概是这样的:
$workflow = $this->getRepository('MyBundle:Workflow')->findById(1);
$inboxes = $workflow->getInboxes();
那么,有没有办法做到这一点?谢谢。更改控制器:
$workflow = $this->getDoctrine()->getRepository('MyBundle:Workflow')->find(1);
$inboxes = $workflow->getInboxes();
别忘了你需要
// Workflow entity
public function __construct()
{
// make `use` statement for this, not long
$this->inboxes = new \Doctrine\Common\Collections\ArrayCollection() ;
}
及
谢谢你的回答,我成功了。但是你所说的“为这个……做
使用语句”是什么意思呢?我记得收到一条错误消息,上面说了关于使用。也许你能帮我澄清一下。再次感谢。在名称空间
之后,放置使用条令\Common\Collections\ArrayCollection
并在uu construct()方法中执行以下操作:$this->inbox=newarraycollection()代码>。
/**
* @ORM\OneToMany(targetEntity="Inbox", mappedBy="workflow", cascade={"persist"})
*/
protected $inboxes ;
public function getInboxes() { return $this->inboxes ; }
// setInboxes(), addInbox(), removeInbox() here