Question

I need to make a "not like" operation in a where. I know that i can do this:

$predicate->like($a,$b);

But i can't find a way to perform a "not like" and other negated like "not in". Is there any way or i will have to make string where?

Thanks.

Était-ce utile?

La solution

As you mention there is no notLike method on where object. But there is literal method where you can pass anything you want:

$sql = new Sql($adapter);
$select = $sql->select();
$select->from('foo')
       ->where->literal('NOT LIKE ?', array('bar'));

echo $select->getSqlString();

The output wille be:

SELECT "foo".* FROM "foo" WHERE NOT LIKE 'bar'

Autres conseils

For reference to those who want to add the Object method of notLike() instead of using literal()

$predicate->notLike( $a, $b );

Create ZF2/Db/Sql/Predicate/NotLike.php

namespace Zend\Db\Sql\Predicate;

class NotLike extends Like {

    protected $specification = '%1$s NOT LIKE %2$s',

}

In ZF2/Db/Sql/Predicate/Predicate.php add

public function notLike( $identifier, $like ) {
    $this->addPredicate( new NotLike( $identifier, $like ), ($this->nextPredicateCombineOperator) ? : $this->defaultCombination  );
    $this->nextPredicateCombineOperator = null;
    return $this;
}

Test

$select = new Select;
$select->from( 'dbTable' )
->columns( array() )
->where->notLike( 'column', '%test%' );

echo $select->getSqlString();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top