This is what I'm working with:
$this->paginate = array(
'order' => array('Job.deadline' => 'ASC'),
'limit' => 500
);
Not all Jobs have a deadline, and at present, with this order, the NULLS appear above those with deadlines, so in effect I have this:
NULL
NULL
NULL
28/06/2012
29/06/2012
04/07/2012
I'm desperate to get this for clarity:
28/06/2012
29/06/2012
04/07/2012
NULL
NULL
NULL
Is there a way I can achieve this through the paginate 'order' options in CakePHP.
Has anyone managed this at all?
Untested, but something like this should do:
'order' => array('ISNULL(Job.deadline)' => 'asc', 'Job.deadline' => 'asc')
Basically this solution in Cake syntax.
Related
I want to order some posts by the number of downloads, DESC, and it seems the articles are random displayed:
$posts = get_posts(array(
‘post_type’ => ‘post’,
‘posts_per_page’ => 12,
‘meta_key’ => ‘post_views_count’,
‘orderby’ => ‘meta_value’,
‘order’ => ‘DESC’
));
But strange, the query from Mysql works just fine:
`SELECT * FROM wp_postmeta WHERE meta_key = ‘post_views_count’ ORDER BY meta_value DESC`
Any help is appreciated, thanks
Meta_Value field stores values as a string. That's why it returns orderby data in alphabetical order.
f.e.
9,8,7,70,6,5,55,4,3,2,1
To get numerical orderby you should use meta_value_num.
$posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => 12,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
And also, in your question text, commas are wrong. It should be ', not ‘
I'm trying to do a SELECT with a SUM but I have a probleme with aliases. CakePHP 3 is used here.
In my controller, I do :
$preparations->find('all',
[ 'fields' => ['SUM(Preparations.qty) as sumqty', 'order_id', 'product_id'],
'conditions' => ['order_id IN ' => $ids],
'contain' => ['Products'],
'group' => 'product_id'
]);
But the query i have with this is :
SELECT SUM(Preparations.qty) as sumqty AS SUM(`Preparations__qty`) AS `sumqty`, Preparations.order_id AS `Preparations__order_id`, Preparations.product_id AS `Preparations__product_id`
SUM() is written twice. How can I resolve this problem ?
Try this
'fields' => ['sumqty'=>'SUM(Preparations.qty)', 'order_id', 'product_id']
I am using LEFT join and as a result getting null values for is_read column in Messages table. I want to keep the nulls at bottom when ordering. I'm using this in paginator. Following is the my code for doing the same:
$this->Paginator->settings = array(
'fields' => array('User.*'),
'joins' => array(
array('table' => 'messages',
'alias' => 'Message',
'type' => 'LEFT',
'conditions' => array(
'User.id = Message.user_from_id'
)
),
),
'limit' => 20,
'group' => array('User.id'),
'order' => array('ISNULL(Message.is_read)' => 'asc','Message.is_read' => 'asc', 'Message.created' => 'asc'),
);
The query Cakephp generates for this is as follows:
SELECT `User`.*, (CONCAT(`User`.`first_name`, ' ', `User`.`last_name`)) AS `User__full_name` FROM `srs_development`.`users` AS `User` LEFT JOIN `srs_development`.`messages` AS `Message` ON (`User`.`id` = `Message`.`user_from_id`) WHERE 1 = 1 GROUP BY `User`.`id` ORDER BY `Message`.`is_read` asc, `Message`.`created` asc LIMIT 20
ISNULL function is getting omitted in the final query.
Also please suggest a way to accomplish this without using custom pagination() if possible.
Aggregate functions didn't work in the order clause when using Pagination component. I tried declaring a virtual field in Message model as:
public $virtualFields = array(
'sortme' => "ISNULL(Message.is_read)",
);
So finally, declaring it as virtual field in the Message model did the job.
Thank you everyone.
I have a query in a CakePHP 2.0 application where it retrieves a list of records for a specific customer and a specific contract, and all within the last three months:
$jobsheets = $this->Jobsheet->find('all', array(
'conditions' => array(
'Jobsheet.contract' => $contractid,
'Jobsheet.deleted' => '0',
'Jobsheet.closed' => '0',
'Jobsheet.jobdate >' => date('Y-m-d', strtotime("-3 month"))
),
'recursive' => 2,
'order' => 'Jobsheet.jobdate DESC'
));
However, this has proven problematic, and I've been asked to implement an alternative. I can write an alternative in the form of an SQL query, but I'd like to stick to CakePHP's query builder as much as I can. If this isn't possible, then I will go for the SQL route. But for now, I'd appreciate an alternative to the date('Y-m-d', strtotime("-3 month")) code.
$jobsheets = $this->Jobsheet->find('all', array(
'conditions' => array(
'Jobsheet.contract' => $contractid,
'Jobsheet.deleted' => '0',
'Jobsheet.closed' => '0',
'Jobsheet.jobdate' => 'DATE_SUB(NOW(), INTERVAL 3 MONTHS)'
),
'recursive' => 2,
'order' => 'Jobsheet.jobdate DESC'
));
I've got a a table with a column request_time. There will be multiple requests and I want to get the latest request of a particular username:
$this->Requests->find('all', 'conditions' => array (
'username' => $username,
'MAX'=> 'request_time'));
The above doesn't work. Is there something in Cakephp or do I need to do my own ->query()?
You can use the following:
$this->Requests->find('first', array('conditions' => array('username' => $username),
'order' => array('id' => 'DESC') ));
Where id is the auto incremented primary key.
This will give you the latest (single) record, if you are using first in find method, or else use all instead.
If you are not using primary key, you can try the following:
$this->Requests->find('first', array('conditions' => array('username' => $username),
'order' => array('request_time' => 'DESC') ));
If you have auto-incremented primary key then you find the latest record of this table try this
$this->Requests->find('find', 'conditions' => array ('username' => $username),
'order' => array('id' => 'DESC')
);