开发者

Help with CakePHP Model Relationships

Three simple tables...

  1. Feedname (eg. News or Events) which are the names of RSS feeds.
  2. Posts that belong to a Feedname
  3. User, that owns all the posts

I want to use the Form helper to automatically give me a select box so that when I add a post I can select which Feedname to assign it to.

It seems like posts belong to both Feedname and User but I can't get the correct combination of belongsTo and hasMany in my model/ .php files. The select box for feedname is shown, but there is nothing in it. Can anyone point me in the right direction?

The tables look like this at the moment:

CREATE TABLE `feednames` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `posts` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `body` text COLLATE utf8_unicode_ci,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  `user_id` int(10) unsigned NOT NULL DEFAULT '1',
  `feedname_id` int(10) unsigned NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `foreign_key` (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) CHARACTER SET latin1 NOT NULL,
  `password` char(40) CHARACTER SET latin1 NOT NULL,
  `group_id` int(11) NOT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

edit - adding the model .php files ...

class Feedname extends AppModel {
    var $name = 'Feedname';

    var $hasMany = array(
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'feedname_id',
            'dependent' => false
        )
    );
}

class Post extends AppModel {
    var $name = 'Post';

    var $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id'
        ),
        'Feedname' => array(
            'foreignKey' => 'feedname_id'
        )
    );
}

class User extends AppModel {
    var $name = 'User';

    var $hasMany = array(
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'user_id',
            'dependent' => false
        )
    );
}

edit - adding SQL dump ** ...

/posts/index.ctp:

SELECT COUNT(*开发者_运维问答) AS count FROM posts AS Post LEFT JOIN users AS User ON (Post.user_id = User.id) LEFT JOIN feednames AS Feedname ON (Post.feedname_id = Feedname.id) WHERE 1 = 1

SELECT Post.id, Post.title, Post.body, Post.created, Post.modified, Post.user_id, Post.feedname_id, User.id, User.username, User.password, User.group_id, User.created, User.modified, Feedname.id, Feedname.name, Feedname.created, Feedname.modified FROM posts AS Post LEFT JOIN users AS User ON (Post.user_id = User.id) LEFT JOIN feednames AS Feedname ON (Post.feedname_id = Feedname.id) WHERE 1 = 1 ORDER BY Post.created DESC LIMIT 10

Please note: /posts/add.ctp does not produce any SQL dump, so it's not getting the select box options from the database, this is what I'm trying to fix with proper model relationships.


Do you have something like this in your controller methods (e.g. the admin_add / admin_edit functions)?

$feednames = $this->Feedname->find('list');
$this->set('feednames', $feednames);

Cake should then automatically populate the select list with these values. Or you can manually set the values with:

$form->input('feedname_id', array('options' => $feednames));


So far I agree :

posts     > belongs to > users
posts     > belongs to > feednames
feednames > has many   > posts
users     > has many   > posts

Just checking, but, did you actually insert data into your tables? Else it would be only logical that your select field is empty. Also, what does the Sql dump say in debug mode?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜