Join 对于许多关系来说,ORM连接原则
我拥有用户实体:Join 对于许多关系来说,ORM连接原则,join,orm,doctrine-orm,Join,Orm,Doctrine Orm,我拥有用户实体: class User extends Entity implements UserInterface, ProviderInterface { /** * @var int * @ORM\Id * @ORM\Column(name="user_id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var \Doctrine\Common\Colle
class User extends Entity implements UserInterface, ProviderInterface
{
/**
* @var int
* @ORM\Id
* @ORM\Column(name="user_id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var \Doctrine\Common\Collections\Collection
* @ORM\ManyToMany(targetEntity="Application\Entity\Child", mappedBy="parents")
*/
protected $children;
...
}
以及子实体:
class Child extends Entity
{
/**
* @var int
* @ORM\Id
* @ORM\Column(name="child_id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var \Application\Entity\Classroom
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Classroom")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="classroom_id", referencedColumnName="classroom_id")
* })
*/
protected $classroom;
/**
* @var \Doctrine\Common\Collections\Collection
* @ORM\ManyToMany(targetEntity="Application\Entity\User", inversedBy="children")
* @ORM\JoinTable(name="child_parent",
* joinColumns={@ORM\JoinColumn(name="child_id", referencedColumnName="child_id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="user_id")}
* )
*/
protected $parents;
...
}
我需要做的是在UserRepository中添加一个条件,以获取其孩子在教室X中的用户。我没有看到任何关于为许多关系生成联接子句的示例。你有什么想法或好的例子吗
提前感谢。最简单的方法是使用DQL查询,我认为这一查询应该接近您想要的:
SELECT u
FROM Application\Entity\User u
JOIN u.children ch
JOIN c.classroom cr
WHERE cr.id = :classroom
GROUP BY u
希望这有帮助