cakephp - Column not found, retrieving data associated model -
i've got following find() function
$this->user->find('all',array( 'conditions' => array('user.id' => $this->auth->user('id')), 'fields' => array('user.id','userrole.id') ));
and following associations defined
// userrole.php class userrole extends appmodel { public $belongsto = array( 'user' => array( 'classname' => 'user', 'foreignkey' => 'user_id' ) ); } // user.php class user extends appmodel { public $hasmany = array( 'userrole' => array( 'classname' => 'userrole', 'foreignkey' => 'user_id', 'dependent' => false, ) ); }
and in app model recursive set -1
public $recursive = -1;
the database tables following:
create table if not exists `user_roles` ( `id` int(10) unsigned not null auto_increment, `event_id` int(10) unsigned not null, `role_id` int(10) unsigned not null, `user_id` int(10) unsigned not null, primary key (`id`) create table if not exists `users` ( `id` int(11) not null auto_increment, `email` varchar(255) collate utf8_unicode_ci not null, `password` varchar(255) collate utf8_unicode_ci not null, `first_name` varchar(255) collate utf8_unicode_ci not null, `last_name` varchar(255) collate utf8_unicode_ci not null, `created` datetime default null, `modified` datetime default null, primary key (`id`), unique key `email` (`email`)
but vollowing error
error: sqlstate[42s22]: column not found: 1054 unknown column 'userrole.id' in 'field list' sql query: select `user`.`id`, `userrole`.`id` `atlanta`.`users` `user` `user`.`id` = 5
i cake can't find userrole.id, did wrong?
either use behavior containable , find() this
$this->user->find('all',array( 'conditions' => array('user.id' => $this->auth->user('id')), 'contain' => array( 'userrole' => array( 'fields'=>array('id') ) ), 'fields' => array('id') ));
or set recursive 1
$this->user->find('all',array( 'conditions' => array('user.id' => $this->auth->user('id')), 'fields' => array('user.id'), // update: unfortunately cant specify associated models fields without behavior 'recursive' => 1 ));
the thing when recursive set -1 or 0 not fetch associated models unless behavior takes care of e.g. containable or linkable.
Comments
Post a Comment