【发布时间】:2014-01-14 08:39:10
【问题描述】:
我对Cake有很深的联想:
User
---- Garage
---- ---- Vehicle
---- ---- ---- VehicleAlbum
检查 VehicleAlbum 是否属于用户的最佳方法是什么? 因为进行递归 3 非常昂贵。我已经研究过包含,这是最好的解决方案吗?
谢谢,乔希。
【问题讨论】:
标签: php cakephp cakephp-2.0 cakephp-2.3
我对Cake有很深的联想:
User
---- Garage
---- ---- Vehicle
---- ---- ---- VehicleAlbum
检查 VehicleAlbum 是否属于用户的最佳方法是什么? 因为进行递归 3 非常昂贵。我已经研究过包含,这是最好的解决方案吗?
谢谢,乔希。
【问题讨论】:
标签: php cakephp cakephp-2.0 cakephp-2.3
没有递归 3 (see book) 这样的东西。
您也不能使用 Containable 来限制基于子条件 (see reasoning) 的查找结果。
我假设您会想做这样的事情(从 Garage 开始以减少所需的一次查询,因为它具有用户 ID 作为字段):
$this->Garage->find('all', array(
'conditions' => array(
'Garage.user_id' => $userId
),
'joins' => array(
array(
'table' => 'vehicles',
'alias' => 'Vehicle',
'type' => 'inner',
'conditions' => array(
'Vehicle.garage_id = Garage.id'
)
),
array(
'table' => 'vehicle_albums',
'alias' => 'VehicleAlbum',
'type' => 'inner',
'conditions' => array(
'VehicleAlbum.vehicle_id = Vehicle.id',
'VehicleAlbum.id' => $vehicleAlbumId
)
)
)
));
如果是所有者,则应返回结果,否则为空。
【讨论】:
不,除非您进行错误查询,否则它不会很昂贵。那么编写一个查询并连接所有四个表并运行解释......然后检查它是昂贵还是便宜。在您的情况下,如果这些表如上所示连接,那么您必须为连接查询付费,除了更改表之间的关系之外别无他法。
【讨论】: