CakePHP关于深度模型关联的条件

Rya*_*yan 0 cakephp conditional-statements contain cakephp-1.3

使用CakePHP 1.3,我有一个Find语句,其条件为第3级深度关联:Hotel> Room> RoomType> name

我希望我的Find语句只返回具有名称"Suite"的RoomTypes的酒店.

我相信以下代码应该可以工作,但它没有,说明SQL语法错误:

$this->Hotel->find('first', array(
    'contain' => array('Room' => array('RoomType')),
    'conditions' => array(
        'Hotel.Room.RoomType.name' => 'Suite' 
    ),
));
Run Code Online (Sandbox Code Playgroud)

请注意,酒店将拥有许多客房,但每间客房仅提供1种RoomType

我已阅读有关在contains语句中使用条件的信息,但这仅限制返回的RoomTypes,而不是整个Hotels.

我在这里发布了类似的问题,但我还没有找到一个似乎可以解决这个问题的问题.

Ric*_*ome 5

您需要手动创建连接和条件作为查询的一部分:

$this->Hotel->find('first', array(
    'contain'=>array(
        'Room'=>array(
            'RoomType'
        )
    ),
    'joins'=>array(
        array(
             'table'=>'rooms',
             'alias'=>'Room',
             'type'=>'inner',
             'conditions'=>array(
                  'Room.hotel_id'=>'Hotel.id'
             )
        ),
        array(
            'table'=>'room_types',
            'alias'=>'RoomType',
            'type'=>'inner',
            'conditions'=>array(
                'RoomType.id'=>'Room.room_type_id',
                'RoomType.name=>'Suit'
            )
        )
    )
);
Run Code Online (Sandbox Code Playgroud)