交响乐 |在查询构建器中外部化 "where"
Symfony | Externalize "where" in a querybuilder
我有一个包含许多功能的存储库:
$this->createQueryBuilder('q')
->where('q.deleted = :deleted')
->setParameter('deleted', false)
->getQuery()
->getResult();
但我必须在许多其他功能中使用这部分 :
->where('q.deleted = :deleted')
->setParameter('deleted', false)
然后做类似的事情:
$this->createQueryBuilder('q')
->checkIfDeleted()
...
可能吗?抱歉,如果它存在,我查看了 QueryBuilder 文档,但我找不到方法。
TY
我想 过滤集合 就是您要找的东西。从Doctrine documentation看这个例子:
$group = $entityManager->find('Group', $groupId);
$userCollection = $group->getUsers();
$criteria = Criteria::create()
->where(Criteria::expr()->eq("birthday", "1982-02-17"))
->orderBy(array("username" => Criteria::ASC))
->setFirstResult(0)
->setMaxResults(20)
;
$birthdayUsers = $userCollection->matching($criteria);
我也发现 this answer 对你有帮助。
标准太棒了,TY 能胜任。为了帮助别人,我是这样做的:
$questionsArray = $this->createQueryBuilder('q')
->getQuery()
->getResult();
$questionsArrayCollection = new ArrayCollection($questionsArray);
// I'm force to do this because the querybuilder returns an Array and I need an ArrayCollection, criteria can be used only on collections.
return $questionsArrayCollection->matching($this->addCriteriaQuestionNotDeleted());
并且:
public function addCriteriaQuestionNotDeleted()
{
return Criteria::create()
->where(Criteria::expr()->eq("deleted", false));
}
我有一个包含许多功能的存储库:
$this->createQueryBuilder('q')
->where('q.deleted = :deleted')
->setParameter('deleted', false)
->getQuery()
->getResult();
但我必须在许多其他功能中使用这部分 :
->where('q.deleted = :deleted')
->setParameter('deleted', false)
然后做类似的事情:
$this->createQueryBuilder('q')
->checkIfDeleted()
...
可能吗?抱歉,如果它存在,我查看了 QueryBuilder 文档,但我找不到方法。
TY
我想 过滤集合 就是您要找的东西。从Doctrine documentation看这个例子:
$group = $entityManager->find('Group', $groupId);
$userCollection = $group->getUsers();
$criteria = Criteria::create()
->where(Criteria::expr()->eq("birthday", "1982-02-17"))
->orderBy(array("username" => Criteria::ASC))
->setFirstResult(0)
->setMaxResults(20)
;
$birthdayUsers = $userCollection->matching($criteria);
我也发现 this answer 对你有帮助。
标准太棒了,TY 能胜任。为了帮助别人,我是这样做的:
$questionsArray = $this->createQueryBuilder('q')
->getQuery()
->getResult();
$questionsArrayCollection = new ArrayCollection($questionsArray);
// I'm force to do this because the querybuilder returns an Array and I need an ArrayCollection, criteria can be used only on collections.
return $questionsArrayCollection->matching($this->addCriteriaQuestionNotDeleted());
并且:
public function addCriteriaQuestionNotDeleted()
{
return Criteria::create()
->where(Criteria::expr()->eq("deleted", false));
}