Monday, November 26, 2012

How to change order only for child elements in find('threaded').

Hello.

I got problem with setting up an order for child elements different than for parent elements.
I have a blog with comments. Users can switch between comments order - comments can either be sorted descending or ascending.
The problem is, when the order is set to descending, also the child elements are sorted descending. That way, the child comments which are answers to parent comments are also shown in a way that the newest one is on top. And I would like to order the child elements by ascending way, even when parents are sorted descending.

I tried to use IF statement in order clause like this:

if($sort=='desc') {
  $order='IF parent_id IS NULL THEN created desc ELSE created asc END';
} else {
  $order='created asc';
}
$comments=$this->Comment->find('threaded',array('conditions'=>array('post_id'=>$p['Post']['id']),
  'fields'=>array('Comment.id','parent_id','comment','created'),
  'order'=>$order));

but I get an error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'parent_id IS NULL THEN created END desc, IF parent_id IS NOT NULL THEN id END as' at line 1

I've read somewhere that the DESC and ASC cannot be used inside IF or CASE statement. How else could the default ordering for whole table be changes to another only for child elements?

Regards & thank you in advance for help.

--
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 post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: