device.php:
var $hasOne = array(
'Monitordevice' => array(
'className' => 'Monitordevice',
'foreignKey' => 'deviceId', // 'deviceId'
'conditions' => '',
)
);
var $hasMany = array(
'CreativeSchedule' => array(
'className' => 'CreativeSchedule',
'foreignKey' => 'deviceId',
'conditions' => '',
)
);
creative_schedules.php
var $belongsTo = array(
'Device' => array(
'className' => 'Device',
'foreignKey' => 'deviceId',
'conditions' => '',
)
);
我称之为:
$ all_devices = $这 - >设备 - > CreativeSchedule->发现( '全部');
设备表未与creative_schedules进行任何连接
而是我得到2个查询:
SELECT `Device`.`id`, ....FROM `devices` AS `Device` LEFT JOIN `monitordevices` AS `Monitordevice` ON (`Monitordevice`.`deviceId` = `Device`.`id`)
和
SELECT `CreativeSchedule`.`id`, ....FROM `creative_schedules` AS `CreativeSchedule` WHERE `CreativeSchedule`.`deviceId` IN (1, 2, 3, 4, 5, 6, 7)
答案 0 :(得分:1)
使用Containable。
在这两个类中,添加:
var $actsAs = array( 'Containable' );
然后你的发现应该是这样的:
$all_devices = $this->Device->CreativeSchedule->find('all', 'contain' => array('Device'));
作为旁注,我不确定你在哪里放置find(),如果该控制器使用CreativeSchedule,但只考虑$ this-> CreativeSchedule->如果你找到(...)因为模型链可以导致性能降低,并且它不会通过它们的关联将查询“链接”在一起。 (我不确定你是否假设。)
在更糟糕的情况下,如果您愿意,可以随时将$ this-> loadModel('CreativeSchedule')放在控制器操作的顶部。