学说发现'不等于'

Jak*_*e N 40 doctrine-orm

我该怎么办

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)

  • 当接受的答案被接受时,这是正确答案:)几年后情况发生了变化! (3认同)
  • 我建议更新为:使用Doctrine \ Common \ Collections \ Criteria; $ expr = Criteria :: expr(); $ criteria = Criteria :: create(); (2认同)

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)


Eme*_*ing 8

我很容易解决这个问题(没有添加方法),所以我将分享:

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)