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
)
)
)
Related
Here is my code in which there is a key for user_id in the posts array but I want to show the username instead of it's id. Where can I write a function like findUserNameById($id) and call it in a view.
<table>
<tr><td>Sr. No.</td><td>User</td><td>Post</td></tr>
<?php
$counter = 1;
foreach($topic['Post'] as $post) {
echo "<tr><td>".$counter."</td><td>".$post['user_id']."</td><td>".$post['body']."</td></tr>";
$counter++;
}
?>
</table>
here's my array:
Array
(
[Topic] => Array
(
[id] => 1
[user_id] => 0
[title] => Some title
[visible] => 1
[created] => 2014-09-20 02:03:42
[modified] => 2014-09-20 22:30:45
)
[User] => Array
(
[id] =>
[username] =>
[password] =>
[full_name] =>
[role] =>
[created] =>
[modified] =>
)
[Post] => Array
(
[0] => Array
(
[id] => 1
[topic_id] => 1
[user_id] => 0
[body] => CakePHP 1
[created] => 2014-09-20 02:08:51
[modified] => 2014-09-20 02:08:51
)
[1] => Array
(
[id] => 2
[topic_id] => 1
[user_id] => 0
[body] => CakePHP 2
[created] => 2014-09-20 02:08:51
[modified] => 2014-09-20 02:08:51
)
)
)
There's key for the user for the topic but that is not available in case of posts but I have the id.
You write a function like that in the User model (i.e., following the fat model, skinny controller pattern).
However, that is overkill since there is already a field method which should meet your needs.
Then you call your method in the controller, and send the data to the view via set
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 containable behavior in my model I have an array like that
[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
)
)
)
How can I display the value of fields in my view? like I want to display the value of
[Post][title]
[Comment][post_id]
[Comment][author]
how can I do this, please help me to do that I m new in cakephp, I don't know how to do that, thanks in advance.
Try this
foreach($posts as $post){
foreach($post['Comment'] as $comment){
/*Display start here*/
$post['Post']['title'];
$comment['post_id'];
$comment['author'];
}
}
Hope this help...
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 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.