Monday, February 22, 2010

Question about relationships, extending the blog tutorial

First of all let me say I'm very new to CakePHP and complex PHP in
general. I based the blog I am working on off the tutorial on the
CakePHP website. I skipped creating ACL or a more complex management
of permissions because as of now the only people who will be posting
content will be Authorized users so using the Auth component in both
of my controllers with an appropriate beforeFilter() function has done
exactly what I needed. What I am trying to do is in my display of
posted articles have $post['Post']['user_id']; look up the in the
users post table the display name, I haven't found a simple answer for
this so that is why I am posting.

I've looked through the cakePHP book online and thought I might need
to do something to link models together but I didn't have any success
with that. I tried having the Post model include var $hasOne = User
but that ended up breaking everything. Any suggestions would be
greatly appreciated.

Below are my table structures, models, and view I am working with

-- Table structure for table `posts`

CREATE TABLE IF NOT EXISTS `posts` (
`id` int(10) unsigned NOT NULL auto_increment,
`user_id` int(11) NOT NULL,
`title` varchar(50) default NULL,
`category` varchar(50) default NULL,
`body` text,
`frontpage` tinyint(1) NOT NULL,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`)
)
-- Table structure for table `users`

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`username` char(50) default NULL,
`password` char(40) default NULL,
`displayname` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)

Post Model:
class Post extends AppModel {
var $name = 'Post';
var $validate = array (
'title' => array(
'rule' => 'notEmpty'
),
'category' => array(
'rule' => 'notEmpty'
),
'body' => array(
'rule' => 'notEmpty'
),
'frontpage' => array(
'rule' => 'numeric'
)
);
}
User model:
class User extends AppModel {
var $name = 'User';
var $validate = array(
'username' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your Username'
),
'displayname' => array(
'rule' => 'notEmpty'
'message' => 'Please enter the name you wish displayed
with your posts'
),
'password' => array(
'rule' => 'notEmpty',
'message' => 'Pelase enter your Password'
)
);
}
index.ctp: This view is on my main page and loops through all the
posts that are tagged to be on the front page

<?php foreach (array_reverse($posts) as $post){if ($post['Post']
['frontpage']==true){ ?>
<?php echo $html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']
['id'])); ?><br/>
<?php echo $post['Post']['category']; ?><br/>
<?php echo $post['Post']['created']; ?><br/>
<?php echo $post['Post']['body']; ?><br/>
<?php echo $post['Post']['user_id']; ?><br/><br/>
<?php }}; ?>

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

No comments: