Tuesday, September 17, 2013

Re: How to order a find result by a specific value

Thanks Ruben,

This is what worked:
cake way-
'order' => array("Tbl.field_name = '{$string}' DESC")

Sql that was called-
ORDER BY `Tbl`.`field_name` = 'my-value-string' DESC


On Monday, September 16, 2013 9:09:06 PM UTC-4, Reuben wrote:
I don't think CakePHP handles case clauses or specific values in order by clauses.

You could just try sending in a literal value to the order by clause.

i.e. 'order' => 'id=5 desc, id asc'

Although I might recommend fully qualifying the table name i.e. 'order' => 'friends.id=5 desc, friends.id asc'. You may have to investigate the anticipated alias for the table name, used in the query.

I've never done ordering like this, so I don't know if it is vendor specific or not.  CakePHP doesn't tend to cater for vendor specific options, unless it's mission critical. i.e. TOP vs LIMIT.

On Tuesday, 17 September 2013 06:43:19 UTC+10, starchild.no1 wrote:
Hi  all,

I'm trying to return a find result in an order specified by a given value..

    function getAllResults($A=1, $B='red'){
       $results = $this->Tbl->find(all, array(
        'conditions' => array('Tbl.foreign_key' => $A),
        'order' => array('Tbl.color' => $B)
       ));
    }

I've seen:  'order' => array("FIELD(Tbl.color, value_1, value_2, value_3)")
But FIELD takes an array and I would like to use just one value=red.
Besides how would I even know where my required value is located in the array( value_1, value_2, value_3) using FIELD unless it's hard coded.

I've also seen:
select * from your_table  order by case when name = 'core' then 1 else 2 end, priority 

And:
select id, name from friends order by id=5 desc, id asc
select id,name from friends order by id=5 desc, id asc

Which is what I really want to do but can't figure out how to write it the cakephp way.

Thanks

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: