我在使用model-> find()调用以解决我构建的belongsTo关联时遇到问题。
以下是协会:
var $belongsTo = array('User' => array('className' =>'User',
'foreignKey' => 'id',
'fields' => array('username')
)
);
以下是查询电话:
$this->set('broadcasters_list', $this->Broadcaster->find('all',
array('fields'=>array('id')
)));
现在有一件非常奇怪的事情;我在Broadcaster模型上有一个分页调用,当调用它时,它会尊重belongsTo关联并从User模型中返回必要的信息。
小心, 利编辑:
我看了一下正在构建的查询,这是我的内容:
对于find命令:
SELECT `Broadcaster`.`id` FROM `broadcasters` AS `Broadcaster` LEFT JOIN `users` AS `User` ON (`Broadcaster`.`id` = `User`.`id`) LEFT JOIN `images` AS `profile_image` ON (`profile_image`.`user_id` = `Broadcaster`.`id` AND image_type = 3) WHERE 1 = 1
对于分页电话:
SELECT `Broadcaster`.`id`, `Broadcaster`.`key_id`, `Broadcaster`.`session_id`, `Broadcaster`.`broadcast_mode`, `Broadcaster`.`next_guest_id`, `Broadcaster`.`one_to_one_credits`, `Broadcaster`.`one_to_many_credits`, `Broadcaster`.`avilable_for_private`, `Broadcaster`.`connect_type`, `Broadcaster`.`commission`, `Broadcaster`.`credits`, `Broadcaster`.`enabled`, `User`.`username`, `profile_image`.`file_id` FROM `broadcasters` AS `Broadcaster` LEFT JOIN `users` AS `User` ON (`Broadcaster`.`id` = `User`.`id`) LEFT JOIN `images` AS `profile_image` ON (`profile_image`.`user_id` = `Broadcaster`.`id` AND image_type = 3) WHERE `enabled` = 1 LIMIT 10
除了要检索的字段外,它们看起来都一样。我错过了那里的东西吗?
答案 0 :(得分:0)
你有foreignKey错了。外键是当前表中链接到外表中键的值,因此在这种情况下应该是user_id
var $belongsTo = array('User' => array('className' =>'User',
'foreignKey' => 'user_id',
'fields' => array('username')
)
);