我该怎么做
WHERE id != 1
在Doctrine中?
我目前有这个。
$this->getDoctrine()->getRepository('MyBundle:Image')->findById(1);
但是如何表示“不等于”?
这可能很傻,但我找不到任何相关的参考资料。
谢谢
我该怎么做
WHERE id != 1
在Doctrine中?
我目前有这个。
$this->getDoctrine()->getRepository('MyBundle:Image')->findById(1);
但是如何表示“不等于”?
这可能很傻,但我找不到任何相关的参考资料。
谢谢
现在有一种方法可以使用Doctrine的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中进行编辑
doctrine: 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([
'type' => 'DoctrineModule\Form\Element\ObjectRadio',
'name' => 'roles',
'options' => [
'label' => _('Roles'),
'object_manager' => $this->getEntityManager(),
'target_class' => 'Application\Entity\Role',
'property' => 'roleId',
'find_method' => [
'name' => 'matching',
'params' => [
'criteria' => Criteria::create()->where(
Criteria::expr()->neq('roleId', 'guest')
)
],
],
],
]);
$query=$this->dm->createQueryBuilder('AppBundle:DocumentName')
->field('fieldName')->notEqual(null);
$data=$query->getQuery()->execute();