简体   繁体   English

通过user_id在symfony中加入

[英]Join by user_id in symfony

How to choose the data by symfony two tables. 如何通过symfony两个表选择数据。 For example there is a table of posts, it is a field user_id (the user who created the post), I do select all posts and passed to the template. 例如,有一个帖子表,它是一个字段user_id(创建帖子的用户),我确实选择了所有帖子并传递给模板。 The template must display the user data from the table users (for each post on your user_id) as this can be done? 模板必须显示表用户的用户数据(针对user_id上的每个帖子),因为可以这样做?

I read the documentation but did not really understand 我看了文档但是不太了解

My query now: 我现在的查询:

   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()));
   }

I wanna get column path of table 'users' by user_id 我想通过user_id获取表'users'的列path

Did you try , in your template, to do : ( let's say you return posts from our controller ) 您是否尝试过在模板中执行以下操作:(假设您从我们的控制器返回了帖子)

{% for post in posts %}
    {{ post.user }}
{% endfor %}

Doctrine load the whole thing by itself, you should see that in your var_dump 教义本身会加载整个东西,您应该在var_dump中看到


Try this in your repository post 在您的存储库帖子中尝试一下

public function myInnerJoin()
{

       $qb = $this->createQueryBuilder('f') 
            ->select('f','g')
            ->innerJoin('f.user', 'g'); 

       return $qb->getQuery()->getArrayResult();
}

and in controller 并在控制器中

$repository = $this->getDoctrine()->getManager()->getRepository('AcmeBundle:..');
$result = $repository->myInnerJoin(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM