I must be doing it wrong...,, obviously...
This is my view in the Posts controller:

        public function view($id = null) {
                $this->Post->id = $id;
                if (!$this->Post->exists()) {
                        throw new NotFoundException(__('Invalid post'));
                }
                $this->set('post', $this->Post->read(null, $id));
                $users = $this->Post->User->find('list');
                $comment = $this->Post->User->find('all', array('contain' =>
'Comment.user_id = User.username')); // This is my attempt, trying to
make it work.
                $this->set(compact('users'));

                $this->addComment();
        }

Which I then try to use $comment['User']['username']; in my view.
It's annoying because it can't be that hard.

On Oct 17, 4:23 pm, euromark <dereurom...@googlemail.com> wrote:
> sry, I misread the codelines for the edit/add action
>
> you are missing the relationship to user
> if you have the belongsTo relation set in your post model
> you still need to fetch it in the find() statement
>
> either use $recursive = 0/1 or better use the containable behavior
> and "contain"=>array('User')
>
> tip: debug your $comment array with pr($comment) to see what you get.
>
> On 17 Okt., 15:54, Mitchell Vargo <mitchellva...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Thanks for your reply.
> > I've tried to get it right for some time now, but I can't manage to
> > make it work.
> > I get this error when I try to use $comment['User']['username']
>
> > Notice (8): Undefined index: User [APP\View\Posts\view.ctp, line 51]
>
> > include - APP\View\Posts\view.ctp, line 51
> > View::_render() - CORE\Cake\View\View.php, line 598
> > View::render() - CORE\Cake\View\View.php, line 365
> > Controller::render() - CORE\Cake\Controller\Controller.php, line 900
> > Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 113
> > Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 88
> > [main] - APP\webroot\index.php, line 93
>
> > On Oct 17, 11:32 am, flosky <flo.kl...@googlemail.com> wrote:
>
> > > Hi Mitchell,
>
> > > I guess that 'user_id' is the foreign key to the users table?
> > > If so, you should add a 'hasOne' relationship in the comments model
> > > (comments have one user).
> > > When that is working, cake automatically gets the corresponding user
> > > data when you use find().
> > > Now you can pass the results to the view and get the username with
> > > $post['User']['username'].
>
> > > -flosky
>
> > > On 17 Okt., 11:03, euromark <dereurom...@googlemail.com> wrote:
>
> > > > did you add
> > > > $this->displayField = 'username';
> > > > to the user model?
>
> > > > On 17 Okt., 02:58, Mitchell Vargo <mitchellva...@gmail.com> wrote:
>
> > > > > Is there an easy way of replacing the user_id to username in a view
> > > > > that looks like this?
>
> > > > > <div class="related">
> > > > >         <h3><?php echo __('Related Comments');?></h3>
> > > > >         <?php if (!empty($post['Comment'])):?>
> > > > >         <table cellpadding = "0" cellspacing = "0">
> > > > >         <tr>
> > > > >                 <th><?php echo __('Id'); ?></th>
> > > > >                 <th><?php echo __('Title'); ?></th>
> > > > >                 <th><?php echo __('Content'); ?></th>
> > > > >                 <th><?php echo __('Username'); ?></th>
> > > > >                 <th class="actions"><?php echo __('Actions');?></th>
> > > > >         </tr>
> > > > >         <?php
> > > > >                 $i = 0;
> > > > >                 foreach ($post['Comment'] as $comment): ?>
> > > > >                 <tr>
> > > > >                         <td><?php echo $comment['id'];?></td>
> > > > >                         <td><?php echo $comment['title'];?></td>
> > > > >                         <td><?php echo $comment['content'];?></td>
> > > > >                         <td><?php echo $comment['user_id']; ?></td> 
> > > > > <------- I want to
> > > > > change this into the users username
> > > > >                         <td class="actions">
> > > > >                                 <?php echo 
> > > > > $this->Html->link(__('View'), array('controller' =>
> > > > > 'comments', 'action' => 'view', $comment['id'])); ?>
> > > > >                                 <?php echo 
> > > > > $this->Html->link(__('Edit'), array('controller' =>
> > > > > 'comments', 'action' => 'edit', $comment['id'])); ?>
> > > > >                                 <?php echo 
> > > > > $this->Form->postLink(__('Delete'), array('controller'
> > > > > => 'comments', 'action' => 'delete', $comment['id']), null, __('Are
> > > > > you sure you want to delete # %s?', $comment['id'])); ?>
> > > > >                         </td>
> > > > >                 </tr>
> > > > >         <?php endforeach; ?>
> > > > >         </table>
> > > > > <?php endif; ?>
> > > > > </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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to