三个简单的表......
我想使用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_id
。User
)LEFT JOIN id
AS feednames
ON(Feedname
。Post
= feedname_id
。{{ 1}})WHERE 1 = 1
选择Feedname
。id
,Post
。id
,Post
。title
,Post
。{{1} },body
。Post
,created
。Post
,modified
。Post
,user_id
。Post
, feedname_id
。User
,id
。User
,username
。User
,password
。User
,{{ 1}}。group_id
,User
。created
,User
。modified
,Feedname
。id
,{{1} }。Feedname
FROM name
AS Feedname
LEFT JOIN created
AS Feedname
ON(modified
。posts
= {{1} }。Post
)LEFT JOIN users
AS User
ON(Post
。user_id
= User
。id
)WHERE 1 = 1 ORDER BY feednames
。Feedname
DESC LIMIT 10
请注意: /posts/add.ctp不会产生任何SQL转储,所以它没有从数据库中获取选择框选项,这是我正在尝试使用正确的模型修复关系。
答案 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转储在调试模式下会说什么?