I have 3 blog tables in my CakePHP application that are linked with a HABTM association following the CakePHP naming conventions: posts - post_tag_links - tags.
I try to get an array with all the posts that are linked to a specific tag (e.g. "design"). This query works for me:
$this->Post->query("
SELECT
Post.id, Post.title FROM posts AS Post
LEFT JOIN
post_tag_links AS PostTagLink ON Post.id = PostTagLink.post_id
LEFT JOIN
tags AS Tag ON Tag.id = PostTagLink.tag_id
WHERE
Tag.slug = 'design'
GROUP BY
Post.id"
);
CakePHP then generates the following query and gave me 4 results:
SELECT
Post.id,
Post.title
FROM
posts AS Post
LEFT JOIN
post_tag_links AS PostTagLink
ON Post.id = PostTagLink.post_id
LEFT JOIN
tags AS Tag
ON Tag.id = PostTagLink.tag_id
WHERE
Tag.slug = 'design'
GROUP BY
Post.id
BUT... to do some best practice, it's better to not use the "query" method. So I tried the "find all" method:
$this->Post->find('all', array(
'fields' => array(
'Post.id',
'Post.title'
),
'joins' => array(
array(
'table' => 'post_tag_links',
'alias' => 'PostTagLink',
'type' => 'LEFT',
'conditions' => array(
'Post.id' => 'PostTagLink.post_id'
)
),
array(
'table' => 'tags',
'alias' => 'Tag',
'type' => 'LEFT',
'conditions' => array(
'Tag.id' => 'PostTagLink.tag_id',
)
)
),
'conditions' => array(
'Tag.slug' => 'design'
),
'group' => 'Post.id'
)
));
CakePHP then generates the following query and gave NO single result:
SELECT
`Post`.`id`,
`Post`.`title`
FROM
`kattenbelletjes`.`posts` AS `Post`
LEFT JOIN
`kattenbelletjes`.`post_tag_links` AS `PostTagLink`
ON (
`Post`.`id` = 'PostTagLink.post_id'
)
LEFT JOIN
`kattenbelletjes`.`tags` AS `Tag`
ON (
`Tag`.`id` = 'PostTagLink.tag_id'
)
WHERE
`Tag`.`slug` = 'design'
GROUP BY
`Post`.`id
After a lot of trial and error, I discovered the problem is the backticks that CakePHP creates when building up that last query.
My question is: what's the difference between the query with the backticks and the one without the backticks? And how can you leave those backticks in CakePHP?
Thanks ;)
The backticks most probably aren't the problem, as all they do is escaping the identifiers. This is a pretty easy find btw.
http://dev.mysql.com/doc/refman/5.7/en/identifiers.html
Using backticks around field names
The actual problem is more likely that you've defined the conditions in the wrong way, what you are doing there is creating string literal comparision conditions, ie
`Post`.`id` = 'PostTagLink.post_id'
Comparing the id column value to the string PostTagLink.post_id will of course fail.
The correct way to define identifier comparisons is to supply the conditon fragment as a single value instead of a key => value set, ie
'conditions' => array(
'Post.id = PostTagLink.post_id'
)
and
'conditions' => array(
'Tag.id = PostTagLink.tag_id'
)
See also
Cookbook > Models > Associations: Linking Models Together > Joining tables
Related
I need to count how many times my date recorded in the meta key:metakey_AMC_data, in format (d-m-Y) it is contained in the database by comparing it with the current date
$mostra_data_corrente = date('d-m-Y');
$query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')");
$conta_risultati = count($query);
and this I can do perfectly.but now my need is to execute the first query by linking another AND, and specify when the term slug is equal to the category of the event (terms taxonomy), obviously the query is incorrect
SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')
AND(slug = 'aperitivi') "
how can i do this?
You can get that count as well. You need to modify query (code) like follow:
$qry = array(
'post_type' => 'post', // mention your post type to narrow down searching through postmeta table
'meta_query' => array(
array(
'meta_key' => 'metakey_AMC_data',
'meta_value' => $mostra_data_corrente,
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy' => 'nameoftaxonomy', // Write the name of taxonomy that you have assinged while you created a CPT (custom post type)
'field' => 'slug',
'terms' => 'aperitivi',
)
)
)
$the_query = WP_Query($qry);
echo $the_query->post_count;
You have to make some necessary changes in above code to suite your requirements. I've added comment where you have to do changes.
I'm working on a WordPress site with a large product database. I need to make a listing of all the products with a single taxonomy term exception. Using wp_query() creates a huge object and takes a long time. Also, for some reason, my arguments will not exclude products from the one taxonomy term I don't want listed. Here is my current query:
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'orderby' => 'post_title',
'numberposts' => -1,
'tax_query' => array(
'taxonomy' => 'product-main-cats',
'field' => 'term_id',
'terms' => array(23),
'operator' => 'NOT IN',
),
);
$prods = new WP_Query( $args );
I want to rewrite this query using the wpdb class so that I only fetch exactly what I need for my listing instead of the huge wp_query() object. The only columns I need from the wp_post table are, ID and post_title. I'm just not sure of the mySQL syntax, especially for excluding the posts related to the single taxonomy term. Can anyone help me rewrite this?
TIA!
You can write this query via $wpdb , I have attached a sql query version below of your WP_Query.
global $wpdb;
$results = $wpdb->get_results( "SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'products' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_title DESC", ARRAY_A );
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.
I have a Listings table with lat/long fields. I'm using the Haversine Formula to calculate the distance (as an alias/virtual field) between an origin point (33.987339, -81.036819) and the lat/long of each Listing and returning the listings with a distance within 10 miles of the origin point.
The following SQL query in phpMyAdmin returns exactly what I expect:
SELECT *, round(3959 * acos(cos(radians(33.987339)) * cos(radians(Listing.lat)) * cos(radians(Listing.long) - radians(-81.036819)) + sin( radians(33.987339)) * sin(radians(Listing.lat))))
AS distance, `Listing`.`id`
FROM `preview_site`.`listings` AS `Listing`
LEFT JOIN `preview_site`.`users` AS `User` ON (`Listing`.`user_id` = `User`.`id`)
LEFT JOIN `preview_site`.`categories` AS `Category` ON (`Listing`.`category_id` = `Category`.`id`)
LEFT JOIN `preview_site`.`states` AS `State` ON (`Listing`.`state_id` = `State`.`id`)
WHERE `Listing`.`status` = 'Active'
HAVING distance < 10
ORDER BY `distance` ASC LIMIT 20
After attempting (and failing several ways) to get the CakePHP code to correctly generate the above SQL, I used this tool to generate the following CakePHP controller code (it gave both Model and Controller options) from the SQL:
$this->Paginator->virtualFields = array(
'distance' => 'round(3959 * acos(cos(radians(33.987339)) * cos(radians(Listing.lat )) * cos(radians(Listing.long) - radians(-81.036819)) + sin(radians(33.987339)) * sin(radians(Listing.lat))))');
$this->Paginator->settings = array(
'fields' => array(
'Listing.*',
'Listing.distance',
'Listing.id',
'Category.*',
'State.*',
'User.*',
),
'joins' => array(
array(
'conditions' => array(
'Listing.user_id = UserJoin.id',
),
'table' => 'users',
'alias' => 'UserJoin',
'type' => 'left',
),
array(
'conditions' => array(
'Listing.category_id = CatJoin.id',
),
'table' => 'categories',
'alias' => 'CatJoin',
'type' => 'left',
),
array(
'conditions' => array(
'Listing.state_id = StateJoin.id',
),
'table' => 'states',
'alias' => 'StateJoin',
'type' => 'left',
),
),
'conditions' => array(
'Listing.status' => 'Active',
),
'order' => array(
'distance' => 'asc',
),
'limit' => '5',
'having' => array(
'distance <' => '10',
),
'contain' => array(
'User',
'Category',
'State',
),
);
$data = $this->Paginator->paginate('Listing');
$this->set('listings', $data);
If I use this code, I get the following error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Listing.distance' in 'field list'
If I change $this->Paginator->virtualFields to $this->Listing->virtualFields (as I could not find any documentation on Paginator actually using the virtualFields method), I don't get any errors and the pagination works fine, but the returned results are not limited by the distance (all Listing records are returned). Here's a snippet of the generated SQL with the distance alias:
SELECT `Listing`.*, `Listing`.`id`, `Category`.*, `State`.*, `User`.*, (round(3959 * acos(cos(radians(33.987339)) * cos(radians(`Listing`.`lat` )) * cos(radians(`Listing`.`long`) - radians(-81.036819)) + sin(radians(33.987339)) * sin(radians(`Listing`.`lat`)))))
AS `Listing__distance`
FROM `preview_site`.`listings` AS `Listing`
Does anyone have any suggestions for how to make this work correctly? ANY help would be greatly appreciated.
I think where your problem is coming from is CakePHP does not recognize "Having", I believe. Since you don't seem to have a Group By, you can just use a regular WHERE and get the same results, in this case, array('conditions' => array('distance <' => 10)) If you do have a Group By though, see the below:
CakePHP: How can I use a "HAVING" operation when building queries with find method?
There are quite a few open tickets regarding virtual fields.
This might well be one of them.
Even though your initial binding to paginator looks off.
You should add virtual fields to the current model, so Listing.
$this->Listing->virtualFields['distance'] = ...
For me, in those scenarios where I could not easily use the virtual field, it helped to manually use the aliased field, so Listing__distance ASC in your order or more importantly in your having clause.
It will also reuse the already calculated field instead of doing it again (even though I don't know if there is a speed improvement here this way). See this.
Also note that it might be cleaner to leverage a behavior to avoid repeating that for other queries (and to keep it DRY):
$this->Listing->setDistanceAsVirtualField($lag, $lng);
And I usually use conditions to limit the distance (no need for having, is there?).
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.