How to display the field when the behaviour is containable? - cakephp

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...

Related

Print arrays by fields Separate

i have arrays like this :
Array
(
[477] => Array
(
[nid] => 477
[changed] => 1510506071
[created] => 1510506071
[title] => mother
[status] => 1
)
[480] => Array
(
[nid] => 480
[changed] => 1510506071
[created] => 1510506071
[title] => hello
[status] => 1
)
[481] => Array
(
[nid] => 481
[changed] => 1510506071
[created] => 1510506071
[title] => name
[status] => 1
)
i need to render arrays by field Separate and use print_r($result[''][nid]) OR print_r($result[]nid) but dont work !
Any help on doing this?
foreach($yourArray as $array){
print_r(array['nid']);
}

How to get username by it's id in a view in CakePHP?

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

Retrieving recursive data with read function at CakePHP

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.

display data from more than one table

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
)
)
)

find value in array cakephp

Hey guys I am trying to get a value from an array in my events_controller.php file.
Event belongsTo Entity and Entity hasMany Event. I need this value to perform some other logic but im really stuck and i know it should be an easy thing to do.
I am trying to get the value of Entity.user_id from this array.
Array
(
[Event] => Array
(
[id] => 19
[entity_id] => 8
[name] => new event
[time_start] => 2011-02-26 19:09:00
[time_end] => 2011-02-26 19:09:00
[dateStart] => 0000-00-00
[dateEnd] => 0000-00-00
[description] => jgiuguygo
[ageRange] => 67
)
[Entity] => Array
(
[id] => 8
[user_id] => 14
[location_id] => 15
[type] => EVENT
)
[Eventfeedback] => Array
(
)
)
the above matrix i obtained with this code:
$value = $this->Event->read();
pr($value);
Now this is as close as I can get...
Array
(
[Entity] => Array
(
[user_id] => 14
)
[Event] => Array
(
[id] => 19
)
[Eventfeedback] => Array
(
)
)
with this code
$value = $this->Event->read('Entity.user_id');
pr($value);
An last try i got this array
Array
(
[Entity] => Array
(
[id] => 1
[user_id] => 11
[location_id] => 8
[type] => sdfsdfdsf
)
[User] => Array
(
[id] => 11
[firstName] => luis
[lastName] => pooya
[username] => admin
[password] => 94c882c8506497a9f031ca5a4db6d0143c97fe45
[role] => admin
[email] => some
)
[Location] => Array
(
[id] => 8
[name] => First Nation University of Canada
[xCoordinate] => 0
[yCoordinate] => 0
)
[Establishment] => Array
(
)
[Event] => Array
(
)
[Vmachine] => Array
(
)
)
with this code
$value = $this->Event->Entity->find('user_id');
pr($value);
Hope someone can help me out. Thanks in advance.Luis
I'm not sure I understood you correctly. But to get user_id in your examples would be like
$value = $this->Event->read('Entity.user_id');
pr($value['Entity']['user_id']);
$event = $this->Event->read();
$userId = $event['Entity']['user_id'];

Resources