我该怎么办
WHERE id != 1
在教义?
到目前为止我有这个
$this->getDoctrine()->getRepository('MyBundle:Image')->findById(1);
但是我该如何做一个“不等于”呢?
这可能很愚蠢,但我找不到任何参考?
谢谢
我该怎么办
WHERE id != 1
在教义?
到目前为止我有这个
$this->getDoctrine()->getRepository('MyBundle:Image')->findById(1);
但是我该如何做一个“不等于”呢?
这可能很愚蠢,但我找不到任何参考?
谢谢
现在有一种方法可以做到这一点,使用 Doctrine's Criteria。
一个完整的例子可以在如何使用具有比较标准的 findBy 方法中看到,但下面是一个简短的答案。
use \Doctrine\Common\Collections\Criteria;
// Add a not equals parameter to your criteria
$criteria = new Criteria();
$criteria->where(Criteria::expr()->neq('prize', 200));
// Find all from the repository matching your criteria
$result = $entityRepository->matching($criteria);
没有内置的方法可以让你做你想做的事。
您必须向存储库添加一个方法,如下所示:
public function getWhatYouWant()
{
$qb = $this->createQueryBuilder('u');
$qb->where('u.id != :identifier')
->setParameter('identifier', 1);
return $qb->getQuery()
->getResult();
}
希望这可以帮助。
为了提供更多的灵活性,我将在我的存储库中添加下一个函数:
public function findByNot($field, $value)
{
$qb = $this->createQueryBuilder('a');
$qb->where($qb->expr()->not($qb->expr()->eq('a.'.$field, '?1')));
$qb->setParameter(1, $value);
return $qb->getQuery()
->getResult();
}
然后,我可以像这样在我的控制器中调用它:
$this->getDoctrine()->getRepository('MyBundle:Image')->findByNot('id', 1);
根据 Luis 的回答,您可以执行更像默认 findBy 方法的操作。
首先,创建一个所有实体都将使用的默认存储库类。
/* $config is the entity manager configuration object. */
$config->setDefaultRepositoryClassName( 'MyCompany\Repository' );
或者你可以在 config.yml 中编辑它
学说:orm:default_repository_class:MyCompany\Repository
然后:
<?php
namespace MyCompany;
use Doctrine\ORM\EntityRepository;
class Repository extends EntityRepository {
public function findByNot( array $criteria, array $orderBy = null, $limit = null, $offset = null )
{
$qb = $this->getEntityManager()->createQueryBuilder();
$expr = $this->getEntityManager()->getExpressionBuilder();
$qb->select( 'entity' )
->from( $this->getEntityName(), 'entity' );
foreach ( $criteria as $field => $value ) {
// IF INTEGER neq, IF NOT notLike
if($this->getEntityManager()->getClassMetadata($this->getEntityName())->getFieldMapping($field)["type"]=="integer") {
$qb->andWhere( $expr->neq( 'entity.' . $field, $value ) );
} else {
$qb->andWhere( $expr->notLike( 'entity.' . $field, $qb->expr()->literal($value) ) );
}
}
if ( $orderBy ) {
foreach ( $orderBy as $field => $order ) {
$qb->addOrderBy( 'entity.' . $field, $order );
}
}
if ( $limit )
$qb->setMaxResults( $limit );
if ( $offset )
$qb->setFirstResult( $offset );
return $qb->getQuery()
->getResult();
}
}
用法和findBy方法一样,例子:
$entityManager->getRepository( 'MyRepo' )->findByNot(
array( 'status' => Status::STATUS_DISABLED )
);
我很容易解决了这个问题(没有添加方法),所以我将分享:
use Doctrine\Common\Collections\Criteria;
$repository->matching( Criteria::create()->where( Criteria::expr()->neq('id', 1) ) );
顺便说一句,我正在使用 Zend Framework 2 中的 Doctrine ORM 模块,我不确定这在任何其他情况下是否兼容。
在我的例子中,我使用了这样的表单元素配置:在单选按钮数组中显示除“guest”之外的所有角色。
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectRadio',
'name' => 'roles',
'options' => array(
'label' => _('Roles'),
'object_manager' => $this->getEntityManager(),
'target_class' => 'Application\Entity\Role',
'property' => 'roleId',
'find_method' => array(
'name' => 'matching',
'params' => array(
'criteria' => Criteria::create()->where(
Criteria::expr()->neq('roleId', 'guest')
),
),
),
),
));
我使用 QueryBuilder 来获取数据,
$query=$this->dm->createQueryBuilder('AppBundle:DocumentName')
->field('fieldName')->notEqual(null);
$data=$query->getQuery()->execute();