Ersin,

You could do 2 queries:

$event = $this->Event->find('first', array('conditions' => array('Event.id' => 
$id)));

$comments = $this->Event->Comment->find('all', array('conditions' => 
array('Comment.event_id' => $id)));

Also take a look : 
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

Andras Kende
http://www.kende.com


On Dec 13, 2011, at 8:26 PM, Ersin Demirtas wrote:

> Hi, I am trying to create an events website with cakephp 1.3, I have
> already created event, comment and user model. The users can add
> comments for events and add events.
> 
> in my event view page I want to show the event with its comments and
> show who added the event and comments for the event.
> 
> 
> I have the following models:
> 
> class Comment extends AppModel {
>       var $name = 'Comment';
>       var $displayField = 'id';
>       //The Associations below have been created with all possible keys,
> those that are not needed can be removed
> 
>       var $belongsTo = array(
>               'Event' => array(
>                       'className' => 'Event',
>                       'foreignKey' => 'event_id'
> 
>               ),
>               'User' => array(
>                       'className' => 'User',
>                       'foreignKey' => 'id'
>               )
>       );
> }
> 
> 
> ---------------------
> 
> 
> class Event extends AppModel {
>       var $name = 'Event';
>       var $displayField = 'title';
>       //The Associations below have been created with all possible keys,
> those that are not needed can be removed
> 
>       var $belongsTo = array(
>               'User' => array(
>                       'className' => 'User',
>                       'foreignKey' => 'user_id',
>                       'conditions' => '',
>                       'fields'   => array('id', 'username'),
>                       'order' => ''
>               )
>       );
> 
>       var $hasMany = array(
>               'Comment' => array(
>                       'className' => 'Comment',
>                       'foreignKey' => 'event_id',
>                       'dependent' => false,
>                       'conditions' => '',
>                       'fields'   => array('user_id', 'comment','created'),
>                       'order' => '',
>                       'limit' => '',
>                       'offset' => '',
>                       'exclusive' => '',
>                       'finderQuery' => '',
>                       'counterQuery' => ''
>               )
>       );
> 
> }
> 
> 
> -------------------
> 
> 
> class User extends AppModel {
>       var $name = 'User';
>       var $displayField = 'username';
>       //The Associations below have been created with all possible keys,
> those that are not needed can be removed
> 
>       var $hasMany = array(
>               'Comment' => array(
>                       'className' => 'Comment',
>                       'foreignKey' => 'user_id',
>                       'dependent' => false,
>                       'conditions' => '',
>                       'fields' => '',
>                       'order' => '',
>                       'limit' => '',
>                       'offset' => '',
>                       'exclusive' => '',
>                       'finderQuery' => '',
>                       'counterQuery' => ''
>               ),
>               'Event' => array(
>                       'className' => 'Event',
>                       'foreignKey' => 'user_id',
>                       'dependent' => false,
>                       'conditions' => '',
>                       'fields' => '',
>                       'order' => '',
>                       'limit' => '',
>                       'offset' => '',
>                       'exclusive' => '',
>                       'finderQuery' => '',
>                       'counterQuery' => ''
>               )
>       );
> 
> }
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> with this model I get the following result:
> 
> 
> 
> 
> 
> Array
> (
>    [Event] => Array
>        (
>            [id] => 1
>            [category_id] => 0
>            [title] => Tinei Mashaya - Zimbabwe Sculpture
>            [body] => Tinei Mashaya is a talented young sculptor from
> Zimbabwe. Using the knowledge and skill of his ancestors, he carves
> vivid and inspired sculptures from the many fine stones found in his
> native country. Tinei's art lingers between sensualism and
> spirituality. Each piece silently speaks of the yearning of the soul
> to break free from the stifling needs of the physical body.
> He says his inspiration comes from his uncontrollable desire to
> solidify ideas and feelings. Tinei owes much of his training an
> encouragement to his brother, Passmore Mashaya, and to Euwitt
> Nyanhongo, who are renowned artists in their own rights. He has had
> his works exhibited in the USA, Austria, Canada, Australia and South
> Africa.
>            [created] => 2011-12-11 23:00:47
>            [modified] => 2011-12-12 01:19:39
>            [user_id] => 1
>            [address] => Riverside Studios
> Riverside Studios Crisp Road Hammersmith London,W6 9RL
>            [tel] => 2032097013
>            [www] => http://www.cakephp.org
>            [startdate] => 2011-07-04
>            [enddate] => 2011-12-30
>            [thumbnail] => 
> http://www.cakephp.org/87dc36fd-6eec-4b33-a314-b97d020c8272.Jpeg
>            [status] => 0
>        )
> 
>    [User] => Array
>        (
>            [id] => 1
>            [username] => ersindemirtas
>        )
> 
>    [Comment] => Array
>        (
>            [0] => Array
>                (
>                    [user_id] => 1
>                    [comment] => Test comment
>                    [created] => 2011-12-12 22:38:29
>                    [event_id] => 1
>                )
> 
>            [1] => Array
>                (
>                    [user_id] => 1
>                    [comment] => I ike this event!!!!
>                    [created] => 2011-12-13 21:38:00
>                    [event_id] => 1
>                )
> 
>            [2] => Array
>                (
>                    [user_id] => 2
>                    [comment] => I wen to old event this is the second
> guy!
>                    [created] => 2011-12-13 21:38:32
>                    [event_id] => 1
>                )
> 
>        )
> 
> )
> 
> 
> For the event it shows who submited but for the comments it doesn't, I
> am not sure if I done the models correctly, it will be very help if
> you can suggest me what to do. Thanks.
> 
> Ersin Demirtas.
> 
> -- 
> Our newest site for the community: CakePHP Video Tutorials 
> http://tv.cakephp.org 
> Check out the new CakePHP Questions site http://ask.cakephp.org and help 
> others with their CakePHP related questions.
> 
> 
> To unsubscribe from this group, send email to
> cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
> http://groups.google.com/group/cake-php

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to