how to use straight_join in cakephp? - mysql

I write codes as seen below when joining two and more tables in cakephp:
$options['joins'] = array(
array(
'table' => 'table2',
'alias' => 'tbl2',
'type' => 'inner',
'conditions' => array(
'tbl1.customer_id = tbl2.customer_id' ,
"tbl2.del_flag = 'N'" ,
)
)
);
That continues with more tables included in the join.
How do I write the code if I wanna use 'straight_join' instead of 'inner join'?
Changing the 'type' above into 'straight' doesn't do anything.
Is it possible in cakephp?
Thank you for the help.

Related

Wordpress WP_Query Multiple Queries

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!

how to add limit in cakephp having joins

following is my code to fetch record from table
$joins=array(
array(
'table' => 'users',
'alias' => 'User',
'type' => 'LEFT',
'conditions' => array(
'User.id= Notification.UserId'
)
)
);
// fetching all records
$returnArray = $this->find('all', array(
'fields' => array('Notification.id','User.Name'),
'joins' => $joins,
'order' => 'Notification.id DESC',
'limit'=>'100',
));
In above code my limit is not working coz i am using joins. can anybody tell me how to use limit with joins in cakephp.
Is there any other method to add limit? PLease tell me ASAP
Thanks in advance
I'm not familiar with Cake, but try an integer instead of string with your limit.
$returnArray = $this->find('all', array(
'fields' => array('Notification.id','User.Name'),
'joins' => $joins,
'order' => 'Notification.id DESC',
'limit'=> 100,
));

ISNULL not working in order in CAKEPHP

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.

how to write inner join query in cakephp

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);

mysql cakephp query to join multiple tables to get sum of columns

I want to run a query which calculate sum for a particular columns. for this i need to join two table that will map the matched records and give the results. Below i paste my query please correct me where i am wrong in my query..
it may be mapping twice so it shows wrong result.
$this->Inventory->find('all',array('joins'=>array(
array('table' => 'items',
'alias' => 'item',
'type' => 'left',
'conditions' => array(
'Inventory.item_id = item.id')
),
array('table' => 'material_owners',
'alias' => 'owner',
'type' => 'left',
'conditions' => array(
'Inventory.material_owner_id = owner.id')
),
array('table' => 'projects',
'alias' => 'project',
'type' => 'left',
'conditions' => array(
'Inventory.project_id = project.id')
),
array('table' => 'material_payments',
'alias' => 'mp',
'type' => 'left',
'conditions' => array(
'Inventory.material_owner_id=mp.material_owner_id'),
),
),
'conditions'=>array('Inventory.project_id'=>$project_id),
'fields' =>array('sum(Inventory.total_amount) as total_amount','sum(mp.paid_amount) as paid_amount','item.name','item.id','owner.id','owner.first_name','owner.last_name','project.id','project.name'),
'group'=> array('item.name','item.id','owner.id','owner.first_name','owner.last_name','project.name','project.id')
)
);
This is the result query generated by cakephp.
SELECT sum("Inventory"."total_amount") as total_amount, sum("mp"."paid_amount") as paid_amount, "item"."name" AS "item__name", "item"."id" AS "item__id", "owner"."id" AS "owner__id", "owner"."first_name" AS "owner__first_name", "owner"."last_name" AS "owner__last_name", "project"."id" AS "project__id", "project"."name" AS "project__name" FROM "inventories" AS "Inventory" left JOIN "items" AS "item" ON ("Inventory"."item_id" = "item"."id") left JOIN "material_owners" AS "owner" ON ("Inventory"."material_owner_id" = "owner"."id") left JOIN "projects" AS "project" ON ("Inventory"."project_id" = "project"."id") left JOIN "material_payments" AS "mp" ON ("Inventory"."material_owner_id" = "mp"."material_owner_id") LEFT JOIN "items" AS "Item" ON ("Inventory"."item_id" = "Item"."id") LEFT JOIN "units" AS "Unit" ON ("Inventory"."unit_id" = "Unit"."id") LEFT JOIN "projects" AS "Project" ON ("Inventory"."project_id" = "Project"."id") LEFT JOIN "material_owners" AS "MaterialOwner" ON ("Inventory"."project_id" = "MaterialOwner"."id") WHERE "Inventory"."project_id" = '4' GROUP BY "item"."name", "item"."id", "owner"."id", "owner"."first_name", "owner"."last_name", "project"."name", "project"."id"
I'm not sure what you're trying to achive here. Does your cake query work at all?
What errors do you get or how do you want the output to be?
For a sollution, I'd take a look at virtualfields.
http://book.cakephp.org/2.0/en/models/virtual-fields.html
They can be added to each model prior to your query with the joins.