Can I pass a string to Zend sql's order function?

122 views Asked by At

I'm writing a complex search algorithm that does the searching in tiers. And for two of the tiers I need very weird order clauses which Zend's sql builder simply couldn't handle. Is there a way to tell Zend to just add a string to the order by clause without it trying to interpret and ruin it?

to those wondering, an example of my order by is

IF(Name LIKE '1234 %' OR Name LIKE '% 1234 %' OR Name LIKE '% 1234' OR Code LIKE '1234 %' OR Code LIKE '% 1234 %' OR Code LIKE '% 1234', 1, 0) + IF(Name LIKE 'ASF %' OR Name LIKE '% ASF %' OR Name LIKE '% ASF' OR Code LIKE 'ASF %' OR Code LIKE '% ASF %' OR Code LIKE '% ASF', 1, 0)

it basically counts the number of words in the search string the current record contains and orders by that.

I want to do something like this

$sql = new Sql($this->adapter);
$select = $sql->select();
$select->from('Table');
$select->where($where);
$select->order($orderString);
1

There are 1 answers

0
blackbishop On

If the argument of the order by clause is a plain string, so it will be considered as just a column name (followed or not by "ASC" or "DESC").

A possible solution is to use the Expression class. Something like this :

 $select->order(array(new \Zend\Db\Sql\Expression($orderString)));