简体   繁体   中英

ZF2 - Zend\Db\Sql\Sql - expression within expression

Let's say I have a simple lookup table with two columns, id and name. I want to select all ids from the table for a given set of names. In addition, I want the comparison of the name column to be case insensitive.

For example, I have an array of lowercase names 'apple','orange','banana' and want to execute a query something like:

SELECT id FROM lookup_table WHERE LOWER(name) IN ('apple','orange','banana').

So far I have the following code to create the statement and correctly bind my array of names:

$sql = new Sql($this->adapter);
*$select = $sql->select();*
$select->from('lookup_table')
       ->columns(array('id'))
       ->where(new \Zend\Db\Sql\Predicate\In('name',array('apple','orange','banana')));

$result = $sql->prepareStatementForSqlObject($select)->execute();

$returnArray = array();
foreach ($result as $r) {
    $returnArray[] = $r['id'];
}

return $returnArray;

What I can't work out how to do is wrap the LOWER() function around the name column in the where clause. I don't think you can pass an expression to the identifier part of the Zend\\Db\\Sql\\Predicate\\In.

Any thoughts?

:wq

您可以使用类似

$select->where->expression('lower(name) IN (?)', array('apple','orange','banana'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM