My\SampleBundle\Entity\Subject:
// ...
manyToOne:
author:
targetEntity: My\UserBundle\Entity\User
joinColumn:
name: owner
referencedColumnName: id
与主题相关的用户可以获得。
$query = $this->getEntityManager()
->createQuery('
SELECT s, a
FROM MySampleBundle:Subject s
LEFT JOIN s.author a
WHERE s.author = :author
GROUP BY s.title'
)
->setParameter("author", $author);
相反,我应该如何从用户信息中获取主题?
例如,用户列表页面中的查询。
$query = $this->getEntityManager()
->createQuery('
SELECT u
FROM MyUserBundle:User u
GROUP BY u.username'
);
此时,我还想获得相关科目的数据。
我使用FOSUserBundle 这是一个应该纠正My \ UserBundle \ Entity的问题吗? 或者从一个orm设置阶段出错了?
答案 0 :(得分:0)
我修改过的程序。
步骤:1添加到My \ UserBundle \ Entity \ User
/**
* @ORM\OneToMany(targetEntity="My\SampleBundle\Entity\Subject", mappedBy="author")
*/
private $subjects;
/**
* Get subjects
*
* @return Doctrine\Common\Collections\Collection
*/
public function getSubjects()
{
return $this->subjects;
}
步骤:2添加' inversedBy'
My\SampleBundle\Entity\Subject:
type: entity
// ...
manyToOne:
author:
targetEntity: My\UserBundle\Entity\User
inversedBy: subjects
joinColumn:
name: owner
referencedColumnName: id
STEP:3查询
$query = $this->getEntityManager()
->createQuery('
SELECT u, s
FROM MyUserBundle:User u
LEFT JOIN u.subjects s
ORDER BY u.username'
);
已获得简便数据。 但这不是完美的解决方案。
当联接表增加时,它会停止回答,并且它将一直处于加载状态 我认为数据不一定太重 这个问题尚不清楚。
另一个问题的例子
$query = $this->getEntityManager()
->createQuery('
SELECT u, s, a, c, i
FROM MyUserBundle:User u
LEFT JOIN u.subjects s
LEFT JOIN u.articles a
LEFT JOIN u.comments c
LEFT JOIN u.images i
// etc...
// It will be in the state of loading all the time
// when join table increases.
ORDER BY u.username'
);