我编写了一个BasicRepository类,以便使用它而不是EntityRepository来添加一些基本的修改,比如删除所有已删除的flaged项。
<?php
namespace AppBundle\Repository;
use AppBundle\DataFixtures\ORM\LoadEventPrioData;
use AppBundle\Entity\Location;
use Doctrine\ORM\EntityRepository;
class BasicRepository extends EntityRepository
{
public function createQueryBuilder($alias, $indexBy = null)
{
$query = parent::createQueryBuilder($alias);
dump(parent::getClassName());
dump($this->getClassName());
if (property_exists($this->getClassName(), 'isDeleted')) {
dump("Ping");
$query->andWhere($alias.'.isDeleted = :false')->setParameter('false', false);
}
else {
dump("Pong");
}
return $query;
}
}
控制器:
...
public function searchAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$meta = new ClassMetadata('AppBundle:Location');
$er = new BasicRepository($em, $meta);
$query = $er->createQueryBuilder('u');
...
我的目标是 - 如果财产&#34;被删除&#34; (boolean)存在于Entity中 - Query应该包含一个额外的Where-Statement。
由于某些奇怪的原因,property_exists总是返回false - 即使该属性在类中退出。
答案 0 :(得分:0)
我明白了。您正在寻找的正确位置是Doctrine Filters。检查此包:https://github.com/DeprecatedPackages/DoctrineFilters#usage
在那里,您可以找到与您的用例完全相同的示例:
<?php
use Doctrine\ORM\Mapping\ClassMetadata;
use Symplify\DoctrineFilters\Contract\Filter\FilterInterface;
final class SoftdeletableFilter implements FilterInterface
{
/**
* {@inheritdoc}
*/
public function addFilterConstraint(ClassMetadata $entity, $alias)
{
if ($entity->getReflectionClass()->hasProperty('isDeleted')) {
return "$alias.isDeleted = 0";
}
return '';
}
}