views:

47

answers:

1

Hi,

I am new to cakephp and have been working through the Apress book "Beginning CakePHP from Novice to Professional" but have ran into a problem after the baking chapter.

(If you've seen my other recent question, you'll know that I had to skip that chapter because I cant get the bake console working on my win7 machine).

The problem is that I don't think the table associations are working properly anymore, even though they used to when I started the blog application example at the start of the book.

The blog example has the following tables:

Users
Posts

The User model: $hasMany = array('Post'); The Post model: $belongsTo = array('User');

I am currently using the scaffold just to test out everything but have a few custom views and a custom add() action for the posts_controller.

The problem is that when I use the add() action for the posts_controller, the id field for the user (from the Users table) isn't put into the user_id foreign key field of the Posts table. Therefore, when I display the post, the "author" part of the view is blank because the ID cannot be found. If I use debug($posts) then the array returned doesn't have any information for user_id of each post and therefore no information in the 'User' array.

I thought user_id was the conventional way of adding an association between tables for cakePHP but it doesn't seem to be working.

Any ideas what I need to do?

Thanks so much in advance,

Infiniti Fizz

P.S. Sorry everyone if I'm overwhelming you with cakephp problems.

P.P.S

Almost forgot, my add.ctp view for posts looks as follows:

<div class="posts form">
 <?=$form->create('Post');?>
 <fieldset>
 <legend>Add Post</legend>
 <?
  e($form->input('name'));
  e($form->input('date'));
  e($form->input('content'));
  e($form->input('User'));
 ?>
 </fieldset>
 <?=$form->end('Submit');?>
</div>
<div class="actions">
 <ul>
  <li><?=$html->link(__('List Posts', true),array('action'=>'index'));?></li>
  <li><?=$html->link(__('List Users', true),array('controller','users', 'action'=>'index'));?></li>
  <li><?=$html->link(__('New User', true),array('controller'=>'users', 'action'=>'add'));?></li>
 </ul>
</div>

And the add() action in posts_controller.php:

function add()
{
   if(!empty($this->data))
   {
    $this->Post->create();
    if($this->Post->save($this->data))
    {
     $this->Session->setFlash('The Post has been saved', true);
     $this->redirect(array('action' => 'index'));
    }
    else
    {
     $this->Session->setFlash('The Post could not be saved. Please try again.', true);
    }
   }
   $users = $this->Post->User->find('list');
   $this->set(compact('users'));
}
+1  A: 

I am also new to cake but as far as I know I think you should change this in add.ctp:

$form->input('User')

to this

$form->input('user_id')

The formhelper takes care to display the input as a dropdown (based on the hasMany-belongsTo references).

sipiatti
Ah that worked, thanks so much, I just assumed that would make a drop-down of user_id values like 1,2,3 etc. but yeah that worked.Well there's the first mistake of that book I've/you've found, better circle that one :)
Infiniti Fizz