Symfony2 DQL如何获取用户信息?
例如,假设提取了一篇文章。Symfony2 DQL如何获取用户信息?,symfony,doctrine,Symfony,Doctrine,例如,假设提取了一篇文章。 用户ID在创建时附加到数据库的项目 $article->setAuthor($user_id); // <- This is number, is not object. 如果它只是一个作者 public function find_all($author) { $query = $this->getEntityManager() ->createQuery('
用户ID在创建时附加到数据库的项目
$article->setAuthor($user_id); // <- This is number, is not object.
如果它只是一个作者
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_用户表关联?
还是可以通过左连接 这只是失败的一个例子
$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);
作者
字段在文章
实体与用户之间定义关系$author = $restult->getAuthor() // User Entity Class Object
$authorName = $author->getName();
谢谢你的建议。如果没有关联,这是不可能的吗?它不能从ID号中提取吗?代码将更长,因为您无法在一次查询中获取此数据。这是可能的,但与编程艺术相冲突。非常感谢。哎哟,哎哟…我在所有的逻辑上都错了。
$author = $restult->getAuthor() // User Entity Class Object
$authorName = $author->getName();