Sunday, March 6, 2011

table associations

I just started learning CakePHP and ran into a problem almost
immediately. I am using the Apress book "Beginning CakePHP" and
created tables for "users" and "posts" (as well as models and
controllers) and use the scaffolding for views. I tested the add new
user (works fine), but the add new post doesn't pull in the users. I
get field labeled "Users" and a drop down list but the drop down is
empty. The book notes that if this doesn't work there is a problem
with the table associations but doesn't suggest any way to
troubleshoot what may be wrong.

I checked the database structure and all the files below several times
and they all look ok to me. I tried it with and without explicitly
declaring the foreign key but doesn't seem to work either way.

The book is obviously from 2008 and I am using Cake 1.3.7 has
something changed since this example was made?

.../models/users.php

<?php
class User extends AppModel {
var $name = 'User';
var $hasMany = array('Post');
}
?>

.../models/posts.php

<?php
class Post extends AppModel {
var $name = 'Post';
var $belongsTo = array('User' => array('className' => 'User',
'foreignKey' => 'user_id') );
}
?>

.../controllers/users_controller.php

<?php
class UsersController extends AppController {
var $name = 'Users';
var $scaffold;
}
?>

.../controllers/posts_controller.php

<?php
class PostsController extends AppController {
var $name = 'Posts';
var $scaffold;
}
?>

--
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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: