我在我的存储库类上有一个方法来获取Article它们的联接(类别,图像...),但并不是每个人Article都有类别或图像,这不能使我得到所有预期的结果,导致我当前的存储库功能仅返回Article具有Categories而Image不是null,而忽略具有null值的内容。
我的Article实体有以下关系。
/**
* @ORM\ManyToMany(targetEntity="App\ArticleBundle\Entity\Category", cascade={"persist"})
*/
private $categories;
/**
* @ORM\ManyToOne(targetEntity="App\ArticleBundle\Entity\Image", cascade={"persist"})
*
*/
private $image;
Run Code Online (Sandbox Code Playgroud)
这是我的存储库功能
public function getArticle($id)
{
$qb = $this->createQueryBuilder('a')
->where('a.id = :theId')
->setParameter('theId', $id)
->join('a.author', 'auth')
->addSelect('auth')
->join('a.categories', 'cat')
->addSelect('cat')
->join('a.image', 'img')
->addSelect('img');
return $qb->getQuery()->getOneOrNullResult();
}
Run Code Online (Sandbox Code Playgroud)
现在我想知道如果我能得到Article具有categories,image或不符合参加一个查询。我想说的是,在使用Doctrine延迟加载时(通过避免查询中的联接),我得到了预期的结果。