Symfony2如何通过DQL获取用户信息?

时间:2012-10-30 10:29:16

标签: symfony doctrine

例如,假设提取了一篇文章 用户ID在创建时附加到数据库的文章。

$article->setAuthor($user_id);    // <- This is number, is not object.

这是需要一些文章的代码。

public function find_all()
{
    $query = $this->getEntityManager()
                  ->createQuery('
                      SELECT a, r FROM MySampleBundle:Article a
                      LEFT JOIN a.reviews r
                      ORDER BY a.date_created DESC'
                  );
    return $query->getResult();
}

如果只是作者。

public function find_all($author)
{
    $query = $this->getEntityManager()
                  ->createQuery('
                      SELECT a, r FROM MySampleBundle:Article a
                      LEFT JOIN a.reviews r
                      WHERE a.author = :author
                      ORDER BY a.date_created DESC'
                  )
                  ->setParameter('author', $author);
    return $query->getResult();
}

此时我还应该做些文章的作者信息呢? 我正在使用FOSUserBundle 我应该将article表与fos_user表相关联吗? 或者是否可以通过左连接?

这只是失败的一个例子。

$query = $this->getEntityManager()
              ->createQuery('
                  SELECT a, r FROM MySampleBundle:Article a
                  LEFT JOIN a.reviews r
                  LEFT JOIN MyUserBundle:User u
                    WITH u.id = a.author
                  WHERE a.author = :author
                  ORDER BY a.date_created DESC'
              )
              ->setParameter('author', $author);

1 个答案:

答案 0 :(得分:0)

  1. 您需要通过Article字段
  2. author实体与用户中定义关系
  3. 然后您就可以执行反向作者数据

    $author = $restult->getAuthor() // User Entity Class Object
    $authorName = $author->getName();