我有一个上课的帖子:
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
*/
class Post
{
const TYPE_TEXT = 1;
const TYPE_PHOTOS = 2;
const TYPE_VIDEO = 3;
/**
* @ORM\OneToMany(targetEntity="Photo", mappedBy="post")
*/
private $photos;
and other properties, methods, etc...
我只想带照片的帖子带回来。
我有一个DQL查询,例如:
$qb = $this->createQueryBuilder('p');
$qb->select('p, postPhotos, etc...')
->leftJoin('p.photos', 'postPhotos')
->leftJoin('p.videos', 'postVideos')
etc...
if ($mediaType != null)
{
switch ($mediaType) {
case Post::TYPE_PHOTOS:
$qb->andWhere('postPhotos != :null')
->setParameter('null', null);
“!=:null”无效,COUNT(postPhotos)也无效(显然是出于聚合原因)。
有没有一种方法我只能指定带回1张或更多照片的帖子?
答案 0 :(得分:0)
快速解答:如果您仅使用join(或innerJoin)替换leftJoin的用法,那么您将得到想要的:只有具有至少1张照片的帖子。
详细信息
如果您查看此有用的SO问答:
...您会发现一些出色的维恩图,显示了左连接和内部连接之间的差异。然后,如果查看Doctrine的Doctrine \ ORM \ QueryBuilder类,您会发现它们具有三种连接方法:
/**
* Creates and adds a join over an entity association to the query.
*
* The entities in the joined association will be fetched as part of the query
* result if the alias used for the joined association is placed in the select
* expressions.
*
* <code>
* $qb = $em->createQueryBuilder()
* ->select('u')
* ->from('User', 'u')
* ->join('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');
* </code>
*
* @param string $join The relationship to join.
* @param string $alias The alias of the join.
* @param string|null $conditionType The condition type constant. Either ON or WITH.
* @param string|null $condition The condition for the join.
* @param string|null $indexBy The index for the join.
*
* @return self
*/
public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{
return $this->innerJoin($join, $alias, $conditionType, $condition, $indexBy);
}
/**
* Creates and adds a join over an entity association to the query.
*
* The entities in the joined association will be fetched as part of the query
* result if the alias used for the joined association is placed in the select
* expressions.
*
* [php]
* $qb = $em->createQueryBuilder()
* ->select('u')
* ->from('User', 'u')
* ->innerJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');
*
* @param string $join The relationship to join.
* @param string $alias The alias of the join.
* @param string|null $conditionType The condition type constant. Either ON or WITH.
* @param string|null $condition The condition for the join.
* @param string|null $indexBy The index for the join.
*
* @return self
*/
public function innerJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{
$parentAlias = substr($join, 0, strpos($join, '.'));
$rootAlias = $this->findRootAlias($alias, $parentAlias);
$join = new Expr\Join(
Expr\Join::INNER_JOIN, $join, $alias, $conditionType, $condition, $indexBy
);
return $this->add('join', [$rootAlias => $join], true);
}
/**
* Creates and adds a left join over an entity association to the query.
*
* The entities in the joined association will be fetched as part of the query
* result if the alias used for the joined association is placed in the select
* expressions.
*
* <code>
* $qb = $em->createQueryBuilder()
* ->select('u')
* ->from('User', 'u')
* ->leftJoin('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1');
* </code>
*
* @param string $join The relationship to join.
* @param string $alias The alias of the join.
* @param string|null $conditionType The condition type constant. Either ON or WITH.
* @param string|null $condition The condition for the join.
* @param string|null $indexBy The index for the join.
*
* @return self
*/
public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{
$parentAlias = substr($join, 0, strpos($join, '.'));
$rootAlias = $this->findRootAlias($alias, $parentAlias);
$join = new Expr\Join(
Expr\Join::LEFT_JOIN, $join, $alias, $conditionType, $condition, $indexBy
);
return $this->add('join', [$rootAlias => $join], true);
}
更改代码以使用innerJoin(或仅联接)将导致Doctrine在生成的SQL中发出INNER JOIN,该联接仅返回联接两侧都存在“某物”的记录,因此,任何没有照片不会包含在结果中。