0

例如,假设获取了一篇文章。
用户 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。
我应该将文章表与 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);
4

1 回答 1

0
  1. 您需要通过字段在Article实体中定义与用户的关系author
  2. 然后您将能够检索作者数据

    $author = $restult->getAuthor() // User Entity Class Object
    $authorName = $author->getName();
    
于 2012-10-30T11:12:29.987 回答