我是新来的。
我知道我可以用这种方式来过滤我的表:
$users = $this->getEntityManager()
->getRepository('MyProject'Domain'User')
->findBy(array('age' => 20, 'surname' => 'foo'));
这将只给我那些年龄为20,姓氏为foo的用户。
然而,我想有一个过滤器,它给我的用户谁有年龄20和姓氏是not foo
。
请建议。
Doctrine在其内置的find*
方法中没有提供NOT
。你必须建立你自己的。
- 创建自定义存储库
use Doctrine'ORM'EntityRepository; //maybe a slightly different path
class CustomRepo extends EntityRepository
{
public function findByAgeNotSurname($age, $surname)
{
$qb = $this->createQueryBuilder('u');
return $qb->where('u.age = :age')
->andWhere('u.surname != :surname')
->setParameters(array('age' => $age, 'surname' => $surname))
->getQuery()
->getResult();
}
}
- 添加存储库到实体
/**
* @ORM'Entity(repositoryClass="Namespace'To'CustomRepo")
*/
class Entity
代码没有经过测试,但应该可以工作。
试试这个:
$users = $this->getEntityManager()
->getRepository('MyProject'Domain'User')
->findBy(array('age' => 20, 'surname !=' => 'foo'));