How to do this query condition in cakephp? - sql

How can I make this query the CakePHP way?
SELECT *
FROM uploaded_sales us, sales s
WHERE us.item_id = s.audience_id
The column item_id of uploaded_sales table is not its primary key.
The column audience_id of sales table is not its primary key too.
I tried this one on my model, I'm not getting any errors but it still returns sales as empty:
$reports = $this->find('all',
array(
'joins' => array(
array(
'table' => 'sales',
'alias' => 'Sale',
'type' => 'left',
'conditions' => array('Sale.audience_id' => 'UploadedSale.item_id')
)),
'conditions' => array(
'UploadedSale.month' => $month,
'UploadedSale.year' => $year,
'UploadedSale.company_id' => $company_id,
'UploadedSale.item_type' => $item_type
),
'fields' => $fields
));
return $reports;

Learn about using the Containable behavior in CakePHP:
http://book.cakephp.org/view/1323/Containable
It will make joins a helluva lot easier.

Related

Existence of posts from complex WP_Query

I have a comparative set of arguments for WP_Query involving a custom field.
On a page I need to say "Are there going to be results?, if so display a link to another page that displays these results, if not ignore" There are between 500 and 1200 posts of this type but could be more in the future. Is there a more efficient or direct way of returning a yes/no to this query?
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner',
'value' => $partner,
'compare' => 'LIKE',
),
),
);
$partner_query = new WP_Query($args);
if ($partner_query->have_posts() ) { [MAKE LINK] }
The link is not made from data returned, we already have that information.
Perhaps directly in the database. My SQL is not up to phrasing the query which in English is SELECT * from wp_posts WHERE post_type = 'product'} AND (JOIN??) post_meta meta_key =
partner AND post_id = a post_id that matches the first part of the query.
And if I did this, would this be more efficient that the WP_Query method?
Use 'posts_per_page' => 1 and add 'no_found_rows' => true and 'fields' => 'ids'. This will return the ID of a matching post, and at the same time avoid the overhead of counting all the matching posts and fetching the entire post contents. Getting just one matching post id is far less work than counting the matching posts. And it's all you need.
Like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => 1,
'no_found_rows' => true,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'partner',
'value' => $partner,
'compare' => 'LIKE',
),
),
);
$partner_query = new WP_Query($args);
if ($partner_query->have_posts() ) { [MAKE LINK] }
no_found_rows means "don't count the found rows", not "don't return any found rows". It's only in the code, not the documentation. Sigh.

Converting SQL query to CakePHP

I have this SQL query that I need to convert to CakePHP. I used this website [http://dogmatic69.com/sql-to-cakephp-find-converter][1] that converts the code but I doesn't work in the way that I want. There is no results.
I think it is because it creates an empty array
here is the SQL code :
SELECT shops.phone1 FROM galleries , albums , shops
WHERE galleries.album_id = albums.id and albums.shop_id = shops.id and galleries.id = 210
and this is the result the website gives me :
$options = array(
'fields' => array(
'shops.phone1',
),
'joins' => array(
array(
),
),
'conditions' => array(
'Gallery.album_id = albums.id',
'albums.shop_id = shops.id',
'Gallery.id' => '210',
),
);
$data = $this->find('all', $options);
but the code doesn't work.
Any Ideas what could be wrong ?
Thanks
There are many ways to achieve this.
If you have defined your associations correctly then you can do this:
$data = $this->Shops->find('all')
->contain(['Galleries','Albums'])
->fields(['Shops.phone1'])
->where(['Galleries.id' => 210]);
Otherwise you can use custom join to generate your query:
$data = $this->Shops->find('all')
->join([
'albums' => [
'table' => 'albums',
'type' => 'INNER', // define your join type here
'conditions' => 'albums.shop_id = Shops.id',
],
'galleries' => [
'table' => 'galleries',
'type' => 'INNER', // define your join type here
'conditions' => 'galleries.album_id=albums.id',
]
])
->select(['Shops.phone1'])
->where(['galleries.id' => 210]);
Further Reading: Cakephp -> Query Builder -> Adding Joins

WP_Query $args and compare operators

I'm having troubles to generate an sql query with operators.Actually wirte this code:
$args = array(
'post_type' => 'listings',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
//CITY
array(
'key' => 'city',
'value' => $city,
'compare' => 'LIKE'
),
//type
array(
'key' => 'type',
'value' => $type,
'compare' => 'LIKE'
),
//PRICE
array(
'key' => 'price',
'value' => array( 100, 5000 ),
'compare' => 'BETWEEN'
),
)
);
This code work fine, but now, i need to get the posts with price between $start and $end, OR price = -1. In other words, post that not have set the price, or set to -1 must show in the query. The others conditions (city, type,etc) must be match with AND
Any ideas? Thanks!
My initial thought is to add a filter onto posts_where or posts_join to inject a custom SQL condition into the query.
Something along these lines:
wp_postmeta.meta_key='price' AND
(wp_postmeta.meta_value='-1' OR
CAST(wp_postmeta.meta_value AS INT) BETWEEN $start AND $end)

like OR operator in Cakephp

I am new to cakephp and I want to add or, and, and like to my existing query.
I want to make a condition like this
WHERE 'Message.user_id = Contact.user_id' AND 'Message.mobileNo LIKE'=>"%Contact.mobileNo" OR LIKE'=>"%Contact.homeNo" OR LIKE'=>"%Contact.workNo"
My query is
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'Message.user_id = Contact.user_id',
'Message.mobileNo = Contact.mobileNo'
),
'type' => 'inner'
)
)
), false);
$this->find('all', array('conditions' => array(),
'fields' => array('DISTINCT mobileNo')));
you can use like below in your existing query.
$this->find('all', array(
'conditions' => array(
'OR' => array(
array(
"Message.mobileNo LIKE" => "%Contact.mobileNo",
),
array(
"Message.mobileNo LIKE" => "%Contact.homeNo",
),
array(
"Message.mobileNo LIKE" => "%Contact.workNo",
)
)
),
'fields' => array('DISTINCT mobileNo')
));
And you can also refer Detail Document for simple search with like
you can use: for "like"
$this->Post->find("all",array('condition'=>array('Author
LIKE'=>"ad%")));
above query will give You the data from table posts where author name starts with "ad".
for "OR"
$this->Post->find("all",array('condition'=>array("OR"=>array('Author
LIKE'=>"ad%",'Author LIKE'=>"bo%"))));
above query will give You the data from table posts where author name starts with "ad" OR "bo"
This blog post can b useful to u as well!

Cakephp 1.3 - Ordering a group of results

I have the following model:
$values = $this->PtlUserdata->find('all', array(
'fields' => array(
'PtlUserdata.field',
'PtlUserdata.value',
'PtlUserdata.timestamp'
),
'conditions' => array(
'PtlUserdata.user_id' => $user->get('id'),
),
'order' => array('PtlUserdata.timestamp' => 'DESC'),
'group' => array('PtlUserdata.field')
));
I'm trying to order the results by timestamp and then group the results by field, so I can get the most recent record with that field name.
Does anybody know how to do this is cakephp?
Thanks in advance for any help, much appreciated :)
This is the default behavior for the database. You will have to come up with some sort of workaround to do it.
An idea: CakePHP: Group by ID and Order by date
Does this work:
$values = $this->PtlUserdata->find('all', array(
'fields' => array(
'PtlUserdata.field',
'PtlUserdata.value',
'max(PtlUserdata.timestamp)'
),
'conditions' => array(
'PtlUserdata.user_id' => $user->get('id'),
'PtlUserdata.timestamp' => 'max(PtlUserdata.timestamp)',
),
'group' => array(
'PtlUserdata.field',
'PtlUserdata.timestamp'
)
));
I haven't had the chance to test this but you definitely need to be grouping by PtlUserdata.timestamp.