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()
Related
I have a Category table with a name field.
I want to be able to overwrite that name using a PartnerCategory Table and join it.
So usually I would get the Category like this:
$options = array(
'contain' => array('PartnerCategory'),
'conditions' => array(
'Category.slug' => $slug,
'Category.status' => true,
)
);
$category = $this->Category->find('first', $options);
And then check, if ParentCategory.name is not empty and replace the Category.name with it.
Overwriting the name with php is tedious so I checked virtual fields.
I can overwrite the Category.name using this:
$this->virtualFields = array(
'name' => 'SELECT name FROM app_partner_categories WHERE category_id = 1 AND partner_id = 60'
);
But if the name in the PartnerCategory table is empty or no mathes are found, the Category.name would be null.
How can I overwrite Category.name only if PartnerCategory.name is found?
Not sure but this could be a possible solution:
$joins = array(array('table' => 'app_partner_categories',
'alias' => 'PartnerCategory',
'type' => 'INNER',
'conditions' => array(
'PartnerCategory.category_id = Category.id'
)
)
);
$options = array(
'fields' => array('IF(PartnerCategory.name IS NULL or PartnerCategory.name = "", Category.name, PartnerCategory.name) as overwritten_name'),
'joins' => $joins,
'conditions' => array(
'Category.slug' => $slug,
'Category.status' => true,
)
);
$category = $this->Category->find('first', $options);
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!
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
);
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);