Php 在symfony中由用户_id加入
如何通过symfony两个表格选择数据。例如,有一个帖子表,它是一个字段user_id(创建帖子的用户),我会选择所有帖子并传递给模板。模板必须显示表users中的用户数据(对于用户id上的每个帖子),因为可以这样做吗 我读了文档,但没有真正理解 我现在的问题是:Php 在symfony中由用户_id加入,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,如何通过symfony两个表格选择数据。例如,有一个帖子表,它是一个字段user_id(创建帖子的用户),我会选择所有帖子并传递给模板。模板必须显示表users中的用户数据(对于用户id上的每个帖子),因为可以这样做吗 我读了文档,但没有真正理解 我现在的问题是: public function indexAction() { $posts = $this->getDoctrine() ->getReposit
public function indexAction()
{
$posts = $this->getDoctrine()
->getRepository('AcmePostBundle:Post');
$queryPosts = $posts->createQueryBuilder('p')
->orderBy('p.id', 'DESC')
->getQuery();
return new Response(var_dump($queryPosts->getResult()));
}
我想按用户id获取表“users”的列
路径
您是否在模板中尝试执行以下操作:
(假设您从我们的控制器返回帖子)
你应该在你的var_转储中看到这一点
在你的存储库帖子中试试这个
public function myInnerJoin()
{
$qb = $this->createQueryBuilder('f')
->select('f','g')
->innerJoin('f.user', 'g');
return $qb->getQuery()->getArrayResult();
}
和在控制器中
$repository = $this->getDoctrine()->getManager()->getRepository('AcmeBundle:..');
$result = $repository->myInnerJoin();
否,缺少$POST的用户。我没有加入,我不知道怎么做我添加了一个如何加入的例子。您也可以根据需要加入leftjoin
$repository = $this->getDoctrine()->getManager()->getRepository('AcmeBundle:..');
$result = $repository->myInnerJoin();