I have an activity table with 3 $belongsTo reference keys. I need the join information from these id's i.e. to show the commenttext - I don't want to store the text twice... (same for post and topic).
Activity table: id | post_id | comment_id | topic_id
In each row only post_id OR comment_id OR topic_id is set, the other two id fields are NULL.
So i.e. if post_id = 55, comment_id = NULL, topic_id = NULL I get this:
Array
(
[0] => Array
(
[Activity] => Array
(
[id] => 1
[post_id] => 55
[comment_id] =>
[topic_id] =>
)
[Post] => Array
(
[id] => 55
[name] => Post #1
[description] => This is Post #1.
...
)
[Comment] => Array
(
[id] =>
[post_id] =>
[titel] =>
[description] =>
...
[created] =>
[modified] =>
)
[Topic] => Array
(
[id] =>
...
[created] =>
[modified] =>
)
)
[1] => Array
(
...
Is there a way to join only if the reference id is NOT NULL? I don't want to kill the empty arrays after the find with a php for-each loop.
Another idea was this database table: id | activitytype_id | refid to join with dynamic binding the necessary table depending on the activitytype_id. - That didn't work as well...
That's what I want - is that possible?
Array
(
[0] => Array
(
[Activity] => Array
(
[id] => 1
[post_id] => 55
[comment_id] =>
[topic_id] =>
)
[Post] => Array
(
[id] => 55
[name] => Post #1
[description] => This is Post #1.
...
)
)
[1] => Array
(
[Activity] => Array
(
[id] => 2
[post_id] =>
[comment_id] => 2
[topic_id] =>
)
[Comment] => Array
(
[id] => 2
[post_id] => 4
[titel] => Blabla
[description] => This is the comment description
...
[created] => 2011-01-01 01:30:00
[modified] => 2011-01-01 01:30:00
)
)
[2] => Array
(
...
Thanks in advance! :-)
You would need to query the database to find out which IDs are null and then query the database a second time to grab the related data.
$activity = $this->Activity->read(null, 1);
// some logic to find foreign key with non-null value
$activity[$model] = $this->{$model}->read(null, $id);
I wouldn't waste your time writing two queries; let CakePHP get all the results in a single query. :)
$activity = $this->Activity->findById(1);
Just add this to your model to filter out empty values from results:
public function afterFind($results, $primary = false) {
return Hash::filter($results);
}
You can always make the joins manually. What you want is to do INNER JOIN instead of the LEFT JOIN done by cake. You can also do an afterFind() to delete this something like this.
in your model where you are using the find method
function afterFind($results){
foreach($results as $k => $result){
if (empty($result['Post']['id'])){
unset($results[$k]['Post']);
}
if (empty($result['Comment']['id'])){
unset($results[$k]['Comment']);
}
if (empty($result['Topic']['id'])){
unset($results[$k]['Topic']);
}
}
}
The join is a more direct solution though.
Related
I need to get multiple records from model, then put them into request->data for a view to render into a form with multiple input fieldsets for example name='data[Applicant][0][display_name]'. name='data[Applicant][1][display_name]'...data value goes in for each applicant.
Actually I've already done what i want, but i do not think it is a good method to do so.
Appreciate if anyone can guide me
foreach ($this->Applicant->data['Applicant'] as $key=>$item){
$data['Applicant'][] = $item['Applicant'];
}
$this->request->data = $data;//set Model to data
$this->set('data' , $this->Applicant->data);
$this->Applicant->data is the following:
Array
(
[Applicant] => Array
(
[0] => Array
(
[Applicant] => Array
(
[id] => 1
[application_id] => 17
[name] => User
[first_name] =>
...
)
)
[1] => Array
(
[Applicant] => Array
(
[id] => 3
[application_id] => 17
[name] =>
[first_name] =>
the following is the desired output (less one level):
Array
(
[Applicant] => Array
(
[0] => Array
(
[id] => 1
[application_id] => 17
[name] => User
[first_name] =>
...
)
[1] => Array
(
[id] => 3
[application_id] => 17
[name] =>
[first_name] =>
thanks
This should suffice:
$this->request->data['Applicant'] = Hash::extract( $this->Applicant->data, 'Applicant.{n}.Applicant' );
I have something like a blog system. Each entry can has comments. Each comment is created by a User.
I am currently using the read function at my 'view' action on the controller to retrieve all the data.
Relationships between models are already created (belongTo, hasMany ...etc)
When the entry view is called, I get something like this:
['Entry'] => Array
(
[id] => 1
[body] => 'xxxxxx'
[...] => ...
)
[Comment] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[body] => This is an example of a comment guys!
[created] => 0000-00-00 00:00:00
)
[1] => Array
(
[id] => 2
[user_id] => 1
[body] => This is the 2nd comment!!!
[created] => 0000-00-00 00:00:00
)
)
Is there any way, with read function, to retrieve also the "recursive" data of Comments such as the user data related to the user_id? (in order to get their names etc.)
I expect something like this:
['Entry'] => Array
(
[id] => 1
[body] => xxxxxx
[...] => ...
)
[Comment] => Array
(
[0] => Array
(
[Comment] => Array
(
[id] => 1
[user_id] => 1
[body] => This is an example of a comment guys!
[created] => 0000-00-00 00:00:00
)
[User] => Array
(
[id] => 1
[username] => myusername
[created] => 0000-00-00 00:00:00
)
)
[1] => Array
(
[Comment] => Array
(
[id] => 1
[user_id] => 2
[body] => fasdfasfaT
[created] => 0000-00-00 00:00:00
)
[User] => Array
(
[id] => 2
[username] => myusername2
[created] => 0000-00-00 00:00:00
)
)
)
Thanks.
Yes, it is.
You can control the depth of associations by the recursive attribute, or rather use the containable behavior to specify exactly which models you want to include. I always enable this behavior for all models in AppModel.php ($actsAs = array('Containable');). Then use it like this:
$this->Entry->find('all', array(
...
'contain' => array('Comment' => 'User')
));
The result will look like this:
['Entry'] => Array
(
[id] => 1
[body] => xxxxxx
[...] => ...
)
[Comment] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[body] => This is an example of a comment guys!
[created] => 0000-00-00 00:00:00
[User] => Array
(
[id] => 1
[username] => myusername
[created] => 0000-00-00 00:00:00
)
)
)
In my experience Cake isn't very efficient querying deep associations. In your case it would generate a query for each Comment to fetch its User. I would avoid it by letting Cake fetch only the comments, then pluck the user ids of the comments, get all the users with those ids in one query, and then add that user info to the original result.
I need to find all the records from an array of id ( $user_id = array(); ) to be saved into a table of notifications, to tell these users that their accounts are activated.
After executed these,
$x = $this->User->find('all',array('conditions'=>array('User.id'=>$user_id)));
$find = Set::extract('/User', $x);
I get this result:
Array(
[0] => Array
(
[User] => Array
(
[id] => 2
[name] => joe
[age] => 13
[class] => D
)
)
[1] => Array
(
[User] => Array
(
[id] => 3
[name] => lambert
[age] => 14
[class] => E
)
)
)
I need to achive the array below by extracting the one above
Array
{
[id] => 2
[name] => joe
[age] => 13
[class] => D
}
Array
{
[id] => 3
[name] => lambert
[age] => 14
[class] => E
}
How to make this happen using CakePHP?
Then, inside my view, how to send multiple records based on the extracted array above?
Is it possible to make a foreach loop inside view?
echo $html->link(__('Send',true),array('controller'=>'users','action'=>'notifications',$user_id),array('class'=>'button'));
Obviously, putting the link inside the loop will result in two links with different ids ($user_id = 2 and $user_id = 3). I don't want that to happen. I want a single link that will submit these ids in one go.
The answer for your first question
$data_final =array();
foreach($findas $d)
{
$data_final[] =$d['User'];
}
then you wil achive
Array
(
[0] => Array
(
[id] => 2
[name] => joe
[age] => 13
[class] => D
)
[1] => Array
(
[id] => 4
[name] => lamber
[age] => 23
[class] => E
)
)
The answer to second question
the way you are doing with url is not a good practice. you should post the records
I am learning cakephp and doing an example.I have many tables in my database such as User,Post,Comment,Friend,Photo and each table has a foreign key user_id.Now I want to display user's photo,his/her friend's posts, comments on those posts,user's friends etc like any social networking site after successfull login.can any one please suggest me how to do that.
Thanks in advance.
You want to use the Containable Behavior to pull back a nested object graph that will have all of those fields.
http://book.cakephp.org/view/1323/Containable
You'll get back results like this (...pulled from the cakephp book)
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => First article
[content] => aaa
[created] => 2008-05-18 00:00:00
)
[Comment] => Array
(
[0] => Array
(
[id] => 1
[post_id] => 1
[author] => Daniel
[email] => dan#example.com
[website] => http://example.com
[comment] => First comment
[created] => 2008-05-18 00:00:00
)
[1] => Array
(
[id] => 2
[post_id] => 1
[author] => Sam
[email] => sam#example.net
[website] => http://example.net
[comment] => Second comment
[created] => 2008-05-18 00:00:00
)
)
[Tag] => Array
(
[0] => Array
(
[id] => 1
[name] => Awesome
)
[1] => Array
(
[id] => 2
[name] => Baking
)
)
)
I have the output array from a $Model->find() query which also pulls data from a hasMany relationship:
Array(
[Parent] => Array
(
[id] => 1
)
[Child] => Array
(
[0] => Array
(
[id] => aaa
[score] => 3
[src] => stage6/tn~4bbb38cc-0018-49bf-96a9-11a0f67883f5.jpg
[parent_id] => 1
)
[1] => Array
(
[id] => bbb
[score] => 5
[src] => stage0/tn~4bbb38cc-00ac-4b25-b074-11a0f67883f5.jpg
[parent_id] => 1
)
[2] => Array
(
[id] => ccc
[score] => 2
[src] => stage4/tn~4bbb38cc-01c8-44bd-b71d-11a0f67883f5.jpg
[parent_id] => 1
)
)
)
I'd like to transform this output into something like this, where the child id is the key to additional child attributes:
Array(
[aaa] => Array
(
[score] => 3
[src] => stage6/tn~4bbb38cc-0018-49bf-96a9-11a0f67883f5.jpg
)
[bbb] => Array
(
[score] => 5
[src] => stage0/tn~4bbb38cc-00ac-4b25-b074-11a0f67883f5.jpg
)
[ccc] => Array
(
[score] => 2
[src] => stage4/tn~4bbb38cc-01c8-44bd-b71d-11a0f67883f5.jpg
)
}
Is there an easy way to use Set::extract, Set::combine, Set::insert, etc. to do this efficiently? I cannot figure it out.
I've used Set::combine to do this a few times, but I can't make an argument for efficiency. As an example, I had a list of volunteers and each of those volunteers belonged to a group. I wanted to retrieve each committee and its volunteers, but as an associative array keyed by the committee name. Here's the code I used to do that:
$volunteers = Set::combine (
$this->Volunteer->Committee->find (
'all',
array ( 'order' => 'display_order' )
),
'{n}.Committee.title',
'{n}.Volunteer'
);
Hopefully that helps. The documentation for Set::combine may help elaborate on the details.
As Rob says, Set::combine is the answer to your question. Here's a sample solution, using the XPath 2.0 syntax:
$parents = $this->Parent->find('all');
$assocChildren = Set::combine( $parents, '/Child/id', '/Child' );