我该怎么办
WHERE id != 1
Run Code Online (Sandbox Code Playgroud)
在学说?
到目前为止我有这个
$this->getDoctrine()->getRepository('MyBundle:Image')->findById(1);
Run Code Online (Sandbox Code Playgroud)
但我怎么做"不等于"?
这可能是愚蠢的,但我找不到任何参考?
谢谢
El *_*obo 49
现在有一种方法可以使用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);
Run Code Online (Sandbox Code Playgroud)
BAD*_*med 34
没有内置方法允许您打算执行的操作.
您必须向存储库添加一个方法,如下所示:
public function getWhatYouWant()
{
$qb = $this->createQueryBuilder('u');
$qb->where('u.id != :identifier')
->setParameter('identifier', 1);
return $qb->getQuery()
->getResult();
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
Lui*_*uis 21
为了提供更多的灵活性,我将下一个函数添加到我的存储库:
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();
}
Run Code Online (Sandbox Code Playgroud)
然后,我可以在我的控制器中调用它,如下所示:
$this->getDoctrine()->getRepository('MyBundle:Image')->findByNot('id', 1);
Run Code Online (Sandbox Code Playgroud)
JCM*_*JCM 12
根据Luis的回答,你可以做一些更像默认的findBy方法.
首先,创建一个将由所有实体使用的默认存储库类.
/* $config is the entity manager configuration object. */
$config->setDefaultRepositoryClassName( 'MyCompany\Repository' );
Run Code Online (Sandbox Code Playgroud)
然后:
<?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();
}
}
Run Code Online (Sandbox Code Playgroud)
用法与findBy方法相同,例如:
$entityManager->getRepository( 'MyRepo' )->findByNot(
array( 'status' => Status::STATUS_DISABLED )
);
Run Code Online (Sandbox Code Playgroud)
我很容易解决这个问题(没有添加方法),所以我将分享:
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')
),
),
),
),
));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93786 次 |
| 最近记录: |