I want to convert following complex mysql into cake's find expression:
SELECT p1, p2
FROM
(
SELECT IFNULL(a.c2, '10') AS p1, IFNULL((SELECT MAX(c.c1) FROM my_table c WHERE c.c1>p1), '30') AS p2
FROM my_table a
WHERE
(
(a.user_id = 2) AND (a.c1 BETWEEN '10' AND '30')
)
) as temp
WHERE p2 > 100
ORDER BY p1;
I tried following
http://dogmatic69.com/sql-to-cakephp-find-converter
but unable to generate the desired expression.
Please help. I really don't know how to handle such complex expressions (I do not prefer to use query in cakephp)
Thanks
Let me just convert your query into the cakephp way
if table a's model is A:
$fields = "IFNULL(A.c2, '10') AS p1, IFNULL((SELECT MAX(C.c1) FROM my_table c WHERE C.c1>p1), '30') AS p2";
$conditions = "A.user_id=2 AND A.c1 BETWEEN '10' AND '30'";
$inner_querry = $this->A->find("all", compact("fields", "conditions"));
$fields = "p1,p2";
$conditions = "p1 IN ($inner_querry) AND p2 IN($inner_query) AND p2 > 100";
$order = "p1";
$query = $this->A->find("all", compact("fields", "conditions", "order"));
debug($query); //check results of for error.
I think that sometimes it's not so wrong to use Model->query(), but let' try to use cake functions.
In fact the only way I see to obtain that particular query is building the subqueries with buildStatement() function (and at the end you still have to call Model->query() so...). But just for fun.
Assuming your model name is MyTable, in your MyTablesController do:
$p1 = "IFNULL(a.c2, '10') AS p1";
$db = $this->MyTable->getDataSource();
$subQuery = $db->buildStatement(
array(
'table' => $db->fullTableName($this->MyTable),
'alias' => 'C',
'fields' => array('MAX(c.c1)'),
'conditions' => array(
"C.c1 > " => 'p1',
)
),
$this->MyTable
);
$p2 = "IFNULL(".$subQuery.") AS p2";
$subQuery = $db->buildStatement(
array(
'table' => $db->fullTableName($this->MyTable),
'alias' => 'A',
'fields' => array($p1, $p2),
'conditions' => array(
"A.user_id" => 2,
"A.c1 BETWEEN ? AND ?" => array(10,30)
)
),
$this->MyTable
);
$query = $db->buildStatement(
array(
'table' => $subQuery,
'alias' => 'test',
'fields' => array('p1', 'p2'),
'conditions' => array(
"p2 > " => 100,
)
),
$this->MyTable
);
Related
I'm trying to come up with a way of querying a CPT by a specific author but also if a meta_key has a specific value.
For example, my CPT has an author ID attached to it as well as a 'recipient' custom field. What i need is all posts of this type by this author as well as all posts of this type where 'recipient' = this author.
(Hope I've explained that well enough!)
I'm usually pretty OK with SQL but just having a random brain fart moment and can't figure this one out. Like I said, any help is much appreciated!
== Solved ==
Thanks to #3pepe3 for suggesting the meta_query parameter of WP_Query. Modified my CPT logic slightly and now have a beautifully working system.
The solution was to add a new meta field for each post containing the original sender's user id, then run two meta_queries to retrieve posts where the current user_id matches either the sender or recipient fields, likes so:
$args = array(
'post_type' => 'activity',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'sender',
'value' => $user_id,
'type' => 'BINARY',
'compare' => '='
),
array(
'key' => 'recipient',
'value' => $user_id,
'type' => 'BINARY',
'compare' => '='
)
)
);
$query = new WP_Query( $args );
Helo JRM47R1X,
Instead of using SQL queries you should use the WP_Query Class
<?php
$args = array(
'post_type' => 'activity',
'author' => $user_id,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'recipient',
'value' => $user_id,
'type' => 'BINARY',
'compare' => '='
),
),
);
$query = new WP_Query($args);
?>
The advantage of using the WP_Query is that you can mix meta queries, taxonomies and dates with logical operands like OR, AND BETWEEN....
Ok I eventually figured it out. Turns out all I needed was a Union on the two queries like so:
SELECT * FROM (
SELECT $wpdb->posts.*
FROM $wpdb->posts
WHERE $wpdb->posts.post_author = $user_id
AND $wpdb->posts.post_type = 'activity'
AND $wpdb->posts.post_status = 'publish'
UNION
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->postmeta.meta_key = 'recipient'
AND $wpdb->postmeta.meta_value = $user_id
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_type = 'activity'
AND $wpdb->posts.post_status = 'publish'
) as x
ORDER BY x.post_date DESC
Hope this helps anyone else having this bizarrely obscure problem!
The following SQL query in phpMyAdmin returns exactly what I expect:
SELECT d.office_id, o.city, sum(d.`nb_followers`) as total_followers FROM `data` as d
LEFT JOIN offices as o on d.office_id = o.id
WHERE d.`year` = 2014 AND d.`month` = 10 AND d.`day` = 01
Group by d.`office_id`
ORDER BY total_followers DESC limit 10
In my Controller :
$this->Paginator->virtualFields = array(
'followers' => 'SUM(Data.nb_followers)',
'id' => 'Office.office_id'
);
$this->Paginator->settings = array(
'fields' => array('id', 'Office.city', 'Data.followers'),
'joins' => array(
array(
'table' => 'data',
'alias' => 'Data',
'type' => 'inner',
'conditions' => array(
'Office.id = Data.office_id',
'year = ' . date('Y', mktime(0, 0, 0, date('m'), 1, date('Y'))),
'month = ' . date('m', mktime(0, 0, 0, date('m'), 1, date('Y'))),
'day = 01'
)
)
),
'group' => array('Data.id'), //Mal positionné à la base mais vérifier si activé n'impacte pas les résultats
'order' => array('Data.followers' => 'desc')
//'limit' => 1
);
$dataTop = $this->paginate('Office');
But I get this Error :
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Data.followers' in 'field list'
SQL Query: SELECT Office.id, Office.city, Data.followers FROM ebd.offices AS Office inner JOIN ebd.data AS Data ON (Office.id = Data.office_id AND year = 2014 AND month = 10 AND day = 01) WHERE 1 = 1 GROUP BY Data.id LIMIT 20
Need Your Help Thank you
I did a test in my web app and putted a virtual field in one of my models. The virtual field was a field from a related table and it seems that it does not work. It outputs an sql error. So i remembered from the documentation the subquery command.
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries
I dont know if this works but i will try to give you an example that might suite your needs
$db = $this->Office->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('followers' => 'SUM(Data.nb_followers)'),
'table' => 'datas',
'alias' => 'Data'
),
$this->office);
$fields[] = $subQuery;
$fields[] = 'id';
$fields[] = 'Office.city';
I hope this guides you to the right direction.
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.
below shown is my mysql query.it works well. but I need to do that in cakephp. how can I convert this into cake php
SELECT pp.product_properties_id,ppv.product_property_value_id FROM product_properties pp
INNER JOIN product_property_values ppv ON pp.product_properties_id = ppv.properties_id
WHERE pp.property_name='Color' AND ppv.properties_value='Blue'
please help me..
The cookbook explains how to do this: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables
$query_options = array();
$query_options['fields'] = array( 'pp.product_properties_id', 'ppv.product_property_value_id' );
$query_options['conditions'] = array( 'pp.property_name' => 'Color' , 'ppv.properties_value' => 'Blue');
$query_options['joins'] = array('table' => 'product_property_values',
'alias' => 'ppv',
'type' => 'INNER',
'conditions' => array(
'ppv.id = pp.ppv_id',
)
);
$result = $this->pp->find('all', $query_options);
Basically i need to have this query done through zend framework.
SELECT k.id AS ID ,k.name AS NAME ,k.ppu_sell AS PRICE, k.type as TYPE FROM `inventory` as k UNION
select m.id AS ID, m.name AS NAME, m.price AS PRICE, 'menu' as TYPE FROM menu as m
Try this:
$select = Zend_Db_Table::getDefaultAdapter()->select();
$select->from(
array('inventory' => 'k'),
array(
'ID' => 'k.id',
'NAME' => 'k.name',
'PRICE' => 'k.ppu_sell',
'TYPE' => 'k.type'));
$selectClone = clone $select;
$select->reset()->from(
array('menu' => 'm'),
array(
'ID' => 'm.id',
'NAME' => 'm.name',
'PRICE' => 'm.price',
'TYPE' => new Zend_Db_Expr("'menu'")));
$select = Zend_Db_Table::getDefaultAdapter()->select()->union(array(
$selectClone, $select
));
Zend Framework Select Objects And UNION()