帮助CakePHP模型关系

时间:2010-09-29 19:21:40

标签: cakephp

三个简单的表......

  1. Feedname(例如,新闻或 事件)这是RSS的名称 饲料。
  2. 属于a的帖子 Feedname
  3. 用户,拥有所有 帖子
  4. 我想使用Form帮助器自动给我一个选择框,这样当我添加帖子时,我可以选择将其分配给哪个Feedname。

    似乎帖子同时属于Feedname和User但我无法在我的model / .php文件中获得belongsTo和hasMany的正确组合。显示了feedname的选择框,但其中没有任何内容。有人能指出我正确的方向吗?

    目前这些表格如下:

    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;
    

    编辑 - 添加模型.php文件 ...

    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
            )
        );
    }
    

    编辑 - 添加SQL转储** ...

    /posts/index.ctp:

    SELECT COUNT(*)AS count FROM posts AS Post LEFT JOIN users AS User ON(Post。{{1 }} = user_idUser)LEFT JOIN id AS feednames ON(FeednamePost = feedname_id。{{ 1}})WHERE 1 = 1

    选择FeednameidPostidPosttitlePost。{{1} },bodyPostcreatedPostmodifiedPostuser_idPostfeedname_idUseridUserusernameUserpasswordUser,{{ 1}}。group_idUsercreatedUsermodifiedFeednameid,{{1} }。Feedname FROM name AS Feedname LEFT JOIN created AS Feedname ON(modifiedposts = {{1} }。Post)LEFT JOIN users AS User ON(Postuser_id = Userid)WHERE 1 = 1 ORDER BY feednamesFeedname DESC LIMIT 10

    请注意: /posts/add.ctp不会产生任何SQL转储,所以它没有从数据库中获取选择框选项,这是我正在尝试使用正确的模型修复关系。

2 个答案:

答案 0 :(得分:2)

你的控制器方法中有这样的东西(例如admin_add / admin_edit函数)吗?

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

然后,Cake应自动使用这些值填充选择列表。或者您可以使用以下方法手动设置值:

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

答案 1 :(得分:0)

到目前为止,我同意:

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

只是检查,但是,您是否真的将数据插入表中?否则,您的选择字段为空是合乎逻辑的。另外,Sql转储在调试模式下会说什么?