How to Group and Order by in cakephp - cakephp

How can I group by fields and Order by DESC in cakephp?
$this->set('messages', $this->Message->find('all',array('conditions' => array('Message.receiver_id'=>$this->Session->read('Auth.User.id')),'group' => array('Message.user_id'),'order' => array('max(Message.created) ASC'))));

Records are fetched in cakePhp depend on a model is
$records = $this->Model->find('all', array(
'conditions' => array(conditons),
'fields' => array(fieldlist),
'group' => array('Model.column'),
'order' => array('Model.column', 'Model.column2 DESC')
));
From your code, you can write as
$data = $this->Message->find('all', array(
'conditions' => array( 'Message.receiver_id'=>$this->Session->read('Auth.User.id')),
'group' => array('Message.user_id'),
'order' => array('max(Message.created) ASC')
));
$this->set('messages', $data);

$records = $this->Model->find('all', array(
'conditions' => array(conditons),
'recursive' => -1,
'fields' => array(fieldlist),
'group' => array('Model.column'),
'order' => array('Model.column', 'Model.column2 DESC')
));
If you want to order by latest Message inserted then instead of sort by created use sort by id like this Model.id DESC'

Related

Use alias with order by in CakePHP

i need to create an select with alias, like this:
SELECT*, (adresses.v1 + adresses.v2) AS total FROM adresses ORDER BY distance DESC
How can i insert this query in this code below?
$objects = $this->Property->find('all', array(
'recursive' => 2,
'reformat' => true,
'conditions' => $conditions,
'order' => $order,
'joins' => $joins,
));
Try:
$objects = $this->Property->find('all', array(
'recursive' => 2,
'reformat' => true,
'conditions' => $conditions,
'order' => $order,
'joins' => $joins,
'fields' => array('*', '(adresses.v1 + adresses.v2) AS total'),
));

cakephp paginate with associated model

User has many User_Questionaries. I want paginate users that have particular questionnaire. I used following pagination for it.
$paginate = array(
'conditions' => array(
'User.role' => IWOA,
'UserQuestionary.questionary_id' => $id
),
'recursive' => 1,
'limit' => 10,
'order' => array(
'name' => 'asc'
),
'contain' => array('UserQuestionary')
);
But it is not create join query. It is showing Unknown column UserQuestionary.questionary_id' in 'where clause'
What is the issue? How can i do it?
Finally I used join query for do this.
$paginate = array(
'conditions' => array(
'Iwoa.role' => IWOA,
),
'joins' => array(
array(
'alias' => 'UserQuestionary',
'table' => 'user_questionaries',
'type' => 'LEFT',
'conditions' => 'UserQuestionary.user_id = Iwoa.id AND UserQuestionary.questionary_id = ' . $id
)
),
'limit' => 10,
'order' => array(
'name' => 'asc'
),
);

Cake php count distinct

How can this Cakephp query be made to count distinct?
$matches = $this->Product->find('count', array(
'conditions' => array(
"Product.brand_id" => $brand['Brand']['id'],
'Product.active' => 1
));
Something like this?
$matches = $this->Product->find('count', array(
'fields' => 'DISTINCT Product.brand_id',
'conditions' => array("Product.brand_id" => $brand['Brand']['id'],
'Product.active' => 1)
));

cakephp find() question

I want to convert following mysql statement into cakephp query syntax.Please help me. Here is my query.SELECT * FROM post WHERE (user_id == $id OR awarded_to = $id ) AND id = $id
Here is my cakephp query. is it wrong.
$this->Post->find('all',
array('conditions' => array('OR' =>
array('Post.user_id' => $id),
array('Post.awarded_to' => $id)
),
'AND' =>
array('Post.id' => $id)
))
This one's on the house, but next time read the docs.
$this->Post->find('all', array(
'conditions' => array(
'OR' => array(
array('Post.user_id' => $id),
array('Post.awarded_to' => $id)
),
'Post.id' => $id
)
));
The 2 OR arrays need to be inside an array themself, and there's no need for AND.
Just add Post.id = $id below the OR condition without you give "and" it's automatic read as and condition...
$this->Post->find('all', array(
'conditions' => array(
'OR' => array('Post.user_id' => $id),
array('Post.awarded_to' => $id)
),
'Post.id' => $id
));
You are very close with your query, just you need little modification like to remove 'AND' in conditions array after 'OR' as:
$this->Post->find('all', array(
'conditions' => array(
'OR' => array(
array('Post.user_id' => $id),
array('Post.awarded_to' => $id)
),
'Post.id' => $id
)
));
Or
$this->Post->find('all', array(
'conditions' => array(
'Post.id' => $id
)
));
The second option will be the best for you if you are passing tables primary key 'id' in AND conditions.

CakePHP: Select DISTINCT in ContainableBehaviour

I am trying to select only distinct related model entries but it seems it doesn't work.
I have this:
$active_questions = $this->Question->find('all', array('conditions' => array('test_id' => $active_tests), 'fields' => array('answer_style_id'), 'contain' => array(
'Answer' => array(
'fields' => array('capital_category_id'),
'CapitalCategory' => array(
'fields' => array('id', 'DISTINCT capital_id', 'DISTINCT category_id', 'delete_flag'),
'Capital' => array(
'fields' => array('id', 'delete_flag')
),
'Category' => array(
'fields' => array('id', 'delete_flag')
)
)
)
)));
But Cake seems to automatically add the associated model key, even id I specified it with a DISTINCT keyword:
Query: SELECT `CapitalCategory`.`id`, DISTINCT `CapitalCategory`.`capital_id`, DISTINCT `CapitalCategory`.`category_id`, `CapitalCategory`.`delete_flag`, `CapitalCategory`.`capital_id`, `CapitalCategory`.`category_id` FROM `capital_categories` AS `CapitalCategory` WHERE `CapitalCategory`.`id` = 217
How do I filter out only DISTINCT capitals or categories? For the current example, Cake returns 20 categories with the same id. I want only one to be returned.
Thank you.
Off the top of my head, the following may work using the 'group' option
$active_questions = $this->Question->find(
'all',
array(
'conditions' => array('Question.test_id' => $active_tests),
'fields' => array('answer_style_id'),
'contain' => array(
'Answer' => array(
'fields' => array('capital_category_id'),
'CapitalCategory' => array(
'fields' => array('id', 'capital_id', 'category_id', 'delete_flag'),
'group' => array('capital_id', 'category_id'),
'Capital' => array(
'fields' => array('id', 'delete_flag')
),
'Category' => array(
'fields' => array('id', 'delete_flag')
)
)
)
)
)
);
Why don't you put 'group' just below 'fields' and not inside 'contain', also you may have to remove 'fields' altogether.

Resources