0

我在获取 model->find() 调用以尊重我建立的 belongsTo 关联时遇到问题。

这是协会:

var $belongsTo = array('User' => array('className'  =>'User',
                                       'foreignKey' => 'id',
                                   'fields'     => array('username')
                                       )
                      );

这是 find 调用:

$this->set('broadcasters_list', $this->Broadcaster->find('all',  
                                                         array('fields'=>array('id')
                                                         )));

现在来说一件非常奇怪的事情;我在 Broadcaster 模型上有一个分页调用,在调用时,它尊重 belongsTo 关联并从 User 模型返回必要的信息。

保重,李

编辑:

我查看了正在构建的查询,这就是我所拥有的:

对于查找命令:

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

除了要检索的字段外,一切看起来都一样。我错过了那里的东西吗?

4

1 回答 1

0

你有foreignKey 错误。外键是当前表中的值,链接到外表中的键,所以在这种情况下它应该是 user_id

var $belongsTo = array('User' => array('className'  =>'User',
                                       'foreignKey' => 'user_id',
                                   'fields'     => array('username')
                                       )
                      );
于 2012-06-30T12:02:59.857 回答