Help with CakePHP Model Relationships -
three simple tables...
- feedname (eg. news or events) names of rss feeds.
- posts belong feedname
- user, owns posts
i want use form helper automatically give me select box when add post can select feedname assign to.
it seems posts belong both feedname , user can't correct combination of belongsto , hasmany in model/ .php files. select box feedname shown, there nothing in it. can point me in right direction?
the tables @ 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 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(*) count posts post left join users user on (post.user_id = user.id) left join feednames feedname on (post.feedname_id = feedname.id) 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 posts post left join users user on (post.user_id = user.id) left join feednames feedname on (post.feedname_id = feedname.id) 1 = 1 order post.created desc limit 10
please note: /posts/add.ctp not produce sql dump, it's not getting select box options database, i'm trying fix proper model relationships.
do have in controller methods (e.g. admin_add / admin_edit functions)?
$feednames = $this->feedname->find('list'); $this->set('feednames', $feednames); cake should automatically populate select list these values. or can manually set values with:
$form->input('feedname_id', array('options' => $feednames));
Comments
Post a Comment