Cake PHP: Find condition on deeper level of recursion - php

My Models are related as follows:
Post BELONGS_TO Parent
Parent HAS_MANY ParentAdmin
Now I wish to find all Parent admins of all post if a post's attribute's value is met. The equivalent sql query is :
SELECT parent_admins.* FROM posts
LEFT JOIN parents AS Parent ON Parent.id = posts.target_id
LEFT JOIN parent_admins ON parent_admins.parent_id = Parent.id
WHERE posts.admin_notification = 0 AND Parent.maxtime > posts.created
Assuming Models are linked in cakephp accordingly and recursive = 2, what will be the condition array in find query?

You can both use the bindModel & unbindModel methods to get the exact query you want or you can use the Containable behavior.
More information about how to create and destroy associations on the fly with CakePHP: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly
And you'll need this array of conditions:
$conditions = array(
'Post.admin_notification' => 0,
'Parent.maxtime >' => 'Post.created'
);

according to your query :-
$conditions=array(
'posts.admin_notification'=>0,
'Parent.maxtime >'=>'posts.created'
);

If your query do the job:
Inside PostsController:
$query = "SELECT parent_admins.* FROM posts
LEFT JOIN parents AS Parent ON Parent.id = posts.target_id
LEFT JOIN parent_admins ON parent_admins.parent_id = Parent.id
WHERE posts.admin_notification = 0 AND Parent.maxtime > posts.created"
$result = $this->Posts->query($query);
$debug($result);

Related

mysql get records with relation in usable structure

Say I have 2 tables. parent & child where a parent can have many children.
I want to get the data in the following structure so:
[
{ parent_id:1, children: [{child_id: 1, parent_id:1},{ child_id:2, parent_id:1}],
{ parent_id:2, children: [{child_id: 3, parent_id:2},{ child_id:4, parent_id:2}],
{ parent_id:3, children: [{child_id: 5, parent_id:3},{ child_id:6, parent_id:3}]
]
JOIN gives me duplicate parent data for each child, and GROUP BY parent_id only gives me 1 child per parent.
Currently my solution is: SELECT * from parent loop through the results and then SELECT * FROM child WHERE parent_id = :parent_id. I feel there has to be a better way.
Is MySQL capable of performing this in fewer queries, if so how would I go about it?
EDIT: Hacky solution based on using GROUP_CONCAT:
$q = "SELECT p.*, GROUP_CONCAT(c.id,'|#|', c.column_2, '|#|', c.column3, '|#|', c.column4 SEPARATOR '###') AS children
FROM parent p
INNER JOIN child c ON p.id = c.parent_id";
$parents= query($q);
foreach ($parents as &$parent) {
$children = explode('###', $parent->children);
foreach ($children as &$child){
$child = array_combine(['id','col1','col2','col3'], explode('|#|', $child));
}
$parent->children = $children;
}
Essentially get the string with separators, explode into records, then explode into columns and remap the column values with hardcoded column names.
Leaving this open to tidier solutions.
If there are no better solutions, which would be more efficient.
1 query:group_concat where php manipulates the data into the desired structure.
n* queries: get all parents, then loop through them and get each parents children.
2 queries: get all parents, get all children, loop through and map them (currently using this)
I think you can use GROUP_CONCAT:
SELECT p.parent_id, GROUP_CONCAT(c.child_id) AS childs
FROM parent p
JOIN child c
ON p.parent_id = c.parent_id
GROUP BY p.parent_id

Select multiple columns from inner join in Silverstripe 3.1

In my events calendar module for Silverstripe 3.1, i have added some additional fields to CalendarEvent. I want to make use of these in my template, but from my researches i have seen that events are exported as being CalendarDateTime so i can't use my additional fields.
I have found that in getStandardEvents function, there is a inner join which i think is causing the problem, but i can't figure it out to join the columns from CalendarEvent
$list = DataList::create('CalendarDateTime')
->filter(array(
'EventID' => [139, 140, 141, 143]
))
->innerJoin('CalendarEvent', "EventID = \"{CalendarEvent}\".\"ID\"")
->innerJoin("SiteTree", "\"SiteTree\".\"ID\" = \"{CalendarEvent}\".\"ID\"")
->where("Recursion != 1");
Note: In my code, i have some of the columns as variables, so i have written them as the values that are reffering to.
Here is the original code:
$list = DataList::create($datetimeClass)
->filter(array(
$relation => $ids
))
->innerJoin($eventClass, "$relation = \"{$eventClass}\".\"ID\"")
->innerJoin("SiteTree", "\"SiteTree\".\"ID\" = \"{$eventClass}\".\"ID\"")
->where("Recursion != 1");
I have tried to add the 'CalendarEvent' as a second parameter inside DataList::create(), but no result. I have the same output.
So how can i select columns from CalendarDateTime and CalendarEvent tables ?
Any help would be appreciated.Thanks
I have found a manual way:
$list = DB::query('SELECT * FROM "CalendarDateTime" INNER JOIN "CalendarEvent" ON "EventID" = "CalendarEvent"."ID" INNER JOIN "SiteTree" ON "SiteTree"."ID" = "CalendarEvent"."ID" WHERE "Recursion" != 1');
Any other solutions are welcome.

How to make nested list from three table?

I have three tables like below:
[Category]->[SubCategory]->[Leaf]
-> means one-to-many relation;
Is it possible to make nested list with just one query?
Actual Query:
Select * from Category
join SubCategory
join Leaf
Intended Output:
Category
--Subcategory
----Leaf
----Leaf
--Subcategory
----Leaf
----Leaf
I know about composite design pattern; but lets say I have the result set from query and I want to manipulate the list above with minimal attempt.
Update
My question is without for loop and sub query, can it be done with the one query above, or what is the best way to generate 3 nested list like above from three tables?
Update:
What is the most optimized way to create a nested list like above from three tables in any database and in any language?
Use this query:
SELECT *
FROM Category c
JOIN SubCategory s ON s.CategoryID = c.ID
JOIN Leaf l ON l.SubCategoryID = s.ID
ORDER BY c.ID, s.ID, l.ID
Then in your PHP loop, print the Category name whenever it changes, print the SubCategory name whenever it changes, and print every Leaf name.
$last_cat = null;
while ($row = fetch()) {
if ($row['CategoryName'] !== $last_cat) {
echo $row['CategoryName'] . "\n";
$last_cat = $row['CategoryName'];
$last_subcat = null;
}
if ($row['SubCategoryName'] != $last_subcat) {
echo "--{$row['SubCategoryName']}\n";
$last_subcat = $row['SubCategoryName'];
}
echo "----{$row['LeafName']\n";
}

Filtering HABTM with AND Condition

I'm trying to filter some car parts depending on the categories they are related to.
A part can have many categories (in the code they are called tags), so I chose the HABTM relation with a join table.
Filtering works so far, but only with an OR condition with cake using the SQL command IN.
But I'm trying to filter only the parts that have all the selected categories, so I need to use an AND condition on the category array.
Here's the extracted code from the controller:
$this->Part->bindModel(array('hasOne' => array('PartsTagsJoin')));
$params['conditions'] = array('AND' => array('PartsTagsJoin.tag_id' => $selectedCats));
$params['group'] = array('Part.id');
$parts = $this->Part->find('all',$params);
$this->set('parts',$parts);
$selectedCats is an array like this: array(1,2,3,4,5);
The SQL output is:
'SELECT `Part`.`id`, `Part`.`name`, `Part`.`image`, `Part`.`image_dir`, `Part`.`time_created`, `Part`.`time_edited`, `Part`.`user_id`, `Part`.`editor_id`, `Part`.`notice`, `User`.`id`, `User`.`username`, `User`.`password`, `User`.`usergroup_id`, `User`.`realname`, `PartsTagsJoin`.`id`, `PartsTagsJoin`.`part_id`, `PartsTagsJoin`.`tag_id`
FROM `c33rdfloor`.`parts` AS `Part`
LEFT JOIN `c33rdfloor`.`users` AS `User` ON (`Part`.`user_id` = `User`.`id`)
LEFT JOIN `c33rdfloor`.`parts_tags_join` AS `PartsTagsJoin` ON (`PartsTagsJoin`.`part_id` = `Part`.`id`)
WHERE `PartsTagsJoin`.`tag_id` IN (1, 4, 8, 24)'
How can I filter the parts that have every id that is committed through the $selectedCats Array.
Thank you in advance for your help.
I've got it working thanks to this blog post:
http://nuts-and-bolts-of-cakephp.com/2008/08/06/habtm-and-join-trickery-with-cakephp/
It seems to be a little tricky to filter entries with all selected tags:
The key in achieving an AND relation is to get the count of the selected cats and match it with the ones of the query inside the group parameter.
This line did it:
$params['group'] = array('Part.id','Part.name HAVING COUNT(*) = '.$numCount);
In the End the code looked like this (for people interested in a solution):
// Unbinds the old hasAndBelongsToMany relation and adds a new relation for the output
$this->Part->unbindModel(array('hasAndBelongsToMany'=>array('PartsTag')));
$this->Part->bindModel(array('hasOne'=>array(
'PartsTagsJoin'=>array(
'foreignKey'=>false,
'type'=>'INNER',
'conditions'=>array('PartsTagsJoin.part_id = Part.id')
),
'PartsTag'=>array(
'foreignKey'=>false,
'type'=>'INNER',
'conditions'=>array(
'PartsTag.id = PartsTagsJoin.tag_id',
'PartsTag.id'=>$selectedCats
)))));
$numCount = count($selectedCats); // count of the selected tags
// groups the entries to the ones that got at least the count of the selected tags -> here the 'AND' magic happens
$params['group'] = array('Part.id','Part.name HAVING COUNT(*) = '.$numCount);
$parts = $this->Part->find('all', $params); // sends the query with the params
$this->set('tagCategories', $categories);
$this->set('selectedCats', $selectedCats);
$this->Part->recursive = 4;
$this->set('parts',$parts);

PHP: Loop over a grouped SQL query array but echo one value once for each “grouping”?

I’ve a small piece of code I’ve tried to wrap my brain around for some hours now.
I’m trying to create a query which retrieves some elements and in the end group them by a selector.
I’ll try to demonstrate: I’m querying all the members in my organization. Each member is a member of a “department” and there can be several members in each department, but only one department for each member.
So I’ve created the query to select all members in my organization group by departments.
When I’m displaying it using a simple foreach($my_query as $q) … I want to output $q->department_name but only once for that department. Right now it says “Marketing” under all five members and then it changes to “HR” for all those members. I’ve tried some different methods such as array_unique, creating a function that checks for the string to see if it is the same and other but with no result.
I could create two foreach loops but if it is at all possible not to I would prefer that because two foreach loops would affect the performance.
Any help or suggestion would be very much appreciated.
Sinerely
- Mestika
--- Edited ---
By the way, my query looks like this:
SELECT *
FROM wp_term_taxonomy AS cat_term_taxonomy
INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id
INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID
INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id
WHERE cat_posts.post_status = 'publish'
AND meta.meta_key = 'active'
AND meta.meta_value = 'active'
AND cat_posts.post_type = 'member'
AND cat_term_taxonomy.taxonomy = 'deparment'
GROUP BY cat_terms.slug, cat_term_relationships.object_id
$prev_department = NULL;
foreach ($my_query AS $q)
{
if ($prev_department != $q->department_name)
echo '<h2>'. htmlentities($q->department_name, ENT_COMPAT, 'UTF-8') .'</h2>';
// ...
$prev_department = $q->department_name;
}

Categories