たとえば、記事がフェッチされたとします。
ユーザー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);