the comment_controller:
<?php
class CommentsController extends AppController {

    var $name = 'Comments';

    function index() {
        $this->Comment->recursive = 0;
        $this->set('comments', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid comment', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('comment', $this->Comment->read(null, $id));
    }




the post_controller is
<?php
class PostsController extends AppController {

    var $name = 'Posts';
    //var $uses = array('Comments');

function allpost() {

$posts = $this->Post->find('all', array(
       'contain' => array('Comment')
));

}
    function index() {
        $this->Post->recursive = 0;
        $this->set('posts', $this->paginate());
        //$grouped_comments = $this->Comments->find('all', array('group' =>
'Comments.post_id'));
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid post', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('post', $this->Post->read(null, $id));
    }


and i get this result, i don't see the comments:

Array
(
[0] => Array
(
[Post] => Array
(
[id] => 1
[set_postsid] => 1
[title] => test1
[body] => bodybodybodybodybotest1test1test1test1dybodybodybodybodybody
[created] => 2011-07-03 00:00:00
[modified] => 2011-07-10 01:25:23
[user_id] => 0
)


)


[1] => Array
(
[Post] => Array
(
[id] => 4
[set_postsid] => 1
[title] => new2
[body] => bodyfwjhjkhkhyjefwefwefwefwefwefwefwefwefbodwefwefwefwef
[created] => 2011-07-03 15:31:40
[modified] => 2011-07-10 01:50:46
[user_id] => 0
)


)

On Sun, Jul 17, 2011 at 10:10 AM, Andras Kende <[email protected]> wrote:

> Thats very simple, but you forget to show us your controller...
>
> In your posts controller you coultd start with something like:
>
> function allpost() {
>
> $posts = $this->Post->find('all', array(
>        'contain' => array('Comment'),
>        'limit => 10
> ));
>
> print_r($posts);
> die;
>
> }
>
> if the print_r shows the correct data with comments then its easy to move
> on the the view...
>
> Andras
>
>
>
> On Jul 16, 2011, at 11:18 PM, Cristi wrote:
>
> > hello,
> > i just baked an application that displays my posts in /views/posts/
> > index.ctp and the comments in  /views/comments/index.ctp.
> > What i am shooting for is displaying the comments and the posts on the
> > same page.
> >
> > Any ideas?
> > Thanks
> >
> > ****I have set a relationship inside the post model like this:
> > <?php
> > class Post extends AppModel {
> >       var $name = 'Post';
> >       var $displayField = 'title';
> >       var $actsAs = array('Containable');
> >       var $validate = array(
> >               'title' => array(
> >                       'notempty' => array(
> >                               'rule' => array('notempty'),
> >                       ),
> >               ),
> >       );
> >       var $hasMany = array(
> >               'Comment' => array(
> >                       'className' => 'Comment',
> >                       'foreignKey' => 'post_id',
> >                       'dependent' => false,
> >                       'conditions' => array('Comment.class'=>'Post')
> >               )
> >       );
> > }
> >
> >
> > ****the views/posts/index.ctp looks like this:
> >
> > <div class="procon_container">
> > <div class="pro_container">
> > <div class="actions">
> >       <h3><?php __('Actions'); ?></h3>
> >       <div >
> >               <div><?php echo $this->Html->link(__('New Post', true),
> > array('action' => 'add'), array('class'=>'alllink black')); ?></div>
> >               <div><?php echo $this->Html->link(__('List Comments',
> true),
> > array('controller' => 'comments', 'action' => 'index'),
> > array('class'=>'alllink black')); ?> </div>
> >               <div><?php echo $this->Html->link(__('New Comment', true),
> > array('controller' => 'comments', 'action' => 'add'),
> > array('class'=>'alllink black')); ?> </div>
> >       </div>
> > </div>
> > <div class="posts index">
> >       <h2>Pro</h2>
> >       <div>
> >       <div class="arrange">
> >                       <div class="sort">Arrange by </div>
> >                       <div class="sort"><?php echo
> $this->Paginator->sort('title');?></
> > div>
> >                       <div class="sort"><?php echo
> $this->Paginator->sort('body');?></
> > div>
> >                       <div class="sort"><?php echo
> $this->Paginator->sort('created');?></
> > div>
> >                       <div class="actions"><?php //__('Actions');?></div>
> >       </div>
> > <?php
> >       $i = 0;
> >       foreach ($posts as $post):
> >               $class = null;
> >               if ($i++ % 2 == 0) {
> >                       $class = 'class="altrow"';
> >               }else {
> >                       $class = 'class="altrow_second"';
> >               }
> >       ?>
> > <?php if($post['Post']['set_postsid'] == 1){?>
> >       <div <?php echo $class;?>>
> >       <div class="post_image">
> >       <img src="http://placehold.it/50x50"/>
> >       </div>
> >       <div class="post_content">
> >               <div class="content_title"><?php echo
> $post['Post']['title']; ?></
> > div>
> >               <div class="date_created"><?php echo date('M j Y, h:i',
> > strtotime($post['Post']['created'])); ?> added by <?php echo
> > $users_username?></div>
> >               <div class="content_body"><?php echo $post['Post']['body'];
> ?></div>
> >               <div class="actions">
> >                       <?php echo $this->Html->link(__('View', true),
> array('action' =>
> > 'view', $post['Post']['id']), array('class'=>'alllink black')); ?>
> >                       <?php if ($admin){ echo
> $this->Html->link(__('Edit', true),
> > array('action' => 'edit', $post['Post']['id']),
> > array('class'=>'alllink black')); }?>
> >                       <?php if ($admin){ echo
> $this->Html->link(__('Delete', true),
> > array('action' => 'delete', $post['Post']['id']),
> > array('class'=>'alllink black'), sprintf(__('Are you sure you want to
> > delete # %s?', true), $post['Post']['id'])); }?>
> >               </div>
> >       </div>
> >       </div>
> > <?php } ?>
> > <?php endforeach; ?>
> >       </div>
> >
> >       <p>
> >       <?php
> >       echo $this->Paginator->counter(array(
> >       'format' => __('Page %page% of %pages%, showing %current% records
> out
> > of %count% total, starting on record %start%, ending on %end%', true)
> >       ));
> >       ?>      </p>
> >
> >       <div class="paging">
> >               <?php echo $this->Paginator->prev('<< ' . __('previous',
> true),
> > array(), null, array('class'=>'disabled'));?>
> >        |      <?php echo $this->Paginator->numbers();?>
> > |
> >               <?php echo $this->Paginator->next(__('next', true) . ' >>',
> array(),
> > null, array('class' => 'disabled'));?>
> >       </div>
> >       </div>
> >       </div>
> >
> >
> > ****and the views/comments/index.ctp:
> >
> > <div class="actions">
> >       <h3><?php __('Actions'); ?></h3>
> >       <ul>
> >               <li><?php echo $this->Html->link(__('New Comment', true),
> > array('action' => 'add')); ?></li>
> >               <li><?php echo $this->Html->link(__('List Posts', true),
> > array('controller' => 'posts', 'action' => 'index')); ?> </li>
> >               <li><?php echo $this->Html->link(__('New Post', true),
> > array('controller' => 'posts', 'action' => 'add')); ?> </li>
> >       </ul>
> > </div>
> > <div class="comments index">
> >       <h2><?php __('Comments');?></h2>
> >       <table cellpadding="0" cellspacing="0">
> >       <tr>
> >                       <th><?php echo $this->Paginator->sort('id');?></th>
> >                       <th><?php echo
> $this->Paginator->sort('post_id');?></th>
> >                       <th><?php echo
> $this->Paginator->sort('name');?></th>
> >                       <th><?php echo
> $this->Paginator->sort('content');?></th>
> >                       <th><?php echo
> $this->Paginator->sort('created');?></th>
> >                       <th><?php echo
> $this->Paginator->sort('modified');?></th>
> >                       <th class="actions"><?php __('Actions');?></th>
> >       </tr>
> >       <?php
> >       $i = 0;
> >       foreach ($comments as $comment):
> >               $class = null;
> >               if ($i++ % 2 == 0) {
> >                       $class = ' class="altrow"';
> >               }
> >       ?>
> >       <tr<?php echo $class;?>>
> >               <td><?php echo $comment['Comment']['id']; ?>&nbsp;</td>
> >               <td>
> >                       <?php echo
> $this->Html->link($comment['Post']['title'],
> > array('controller' => 'posts', 'action' => 'view', $comment['Post']
> > ['id'])); ?>
> >               </td>
> >               <td><?php echo $comment['Comment']['name']; ?>&nbsp;</td>
> >               <td><?php echo $comment['Comment']['content'];
> ?>&nbsp;</td>
> >               <td><?php echo $comment['Comment']['created'];
> ?>&nbsp;</td>
> >               <td><?php echo $comment['Comment']['modified'];
> ?>&nbsp;</td>
> >               <td class="actions">
> >                       <?php echo $this->Html->link(__('View', true),
> array('action' =>
> > 'view', $comment['Comment']['id'])); ?>
> >                       <?php echo $this->Html->link(__('Edit', true),
> array('action' =>
> > 'edit', $comment['Comment']['id'])); ?>
> >                       <?php echo $this->Html->link(__('Delete', true),
> array('action' =>
> > 'delete', $comment['Comment']['id']), null, sprintf(__('Are you sure
> > you want to delete # %s?', true), $comment['Comment']['id'])); ?>
> >               </td>
> >       </tr>
> > <?php endforeach; ?>
> >       </table>
> >       <p>
> >       <?php
> >       echo $this->Paginator->counter(array(
> >       'format' => __('Page %page% of %pages%, showing %current% records
> out
> > of %count% total, starting on record %start%, ending on %end%', true)
> >       ));
> >       ?>      </p>
> >
> >       <div class="paging">
> >               <?php echo $this->Paginator->prev('<< ' . __('previous',
> true),
> > array(), null, array('class'=>'disabled'));?>
> >        |      <?php echo $this->Paginator->numbers();?>
> > |
> >               <?php echo $this->Paginator->next(__('next', true) . ' >>',
> array(),
> > null, array('class' => 'disabled'));?>
> >       </div>
> > </div>
> >
> > --
> > 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
> > [email protected] 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
> [email protected] For more options, visit this group
> at http://groups.google.com/group/cake-php
>



-- 
PatrioticCow

-- 
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
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to