2011-07-16 56 views
0

我嘗試在cakePHP 1.3中有許多關聯的模型上找到('all'...),該關係確實具有查詢的過濾條件模式內遞歸的第二級。簡單地說,它看起來像這樣,我要篩選的用戶名:cakePHP - 無法通過belongsTo在第二次遞歸模型關聯

Delivery belongsTo Order, Order belongsTo User. 

這裏是ASSOCS:

Order: var $belongsTo = array(

'User' => array(
     'className' => 'User', 
     'foreignKey' => 'user_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
),.... 

Delivery: var $belongsTo = array(

'Order' => array(
     'className' => 'Order', 
     'foreignKey' => 'order_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ),... 

產生的錯誤是:

SQL Error: 1054: Unknown column 'User.id' in 'where clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 684]

這裏完整查詢,只是爲了好玩:

SELECT Delivery . id , Delivery . order_id , Delivery . delivery_address_id , Delivery . deliver_date , Delivery . created , Delivery . modified , Delivery . deliver_run , Delivery . product_mix_id1 , Delivery . product_mix_id2 , Delivery . product_mix_id3 , Delivery . product_mix_id4 , Delivery . assembled , Delivery . shipped , Delivery . rated , Delivery . price , Delivery . product_lines_id , Order . id , Order . user_id , Order . product_lines_id , Order . order_date , Order . deliver_monday , Order . deliver_tuesday , Order . deliver_wednessday , Order . deliver_thursday , Order . deliver_friday , Order . deliver_saturday , Order . delivery_address_id , Order . payment_delay , Order . active , Order . cancle_date , Order . replaced_order_id , Order . created , Order . modified , DeliveryAddress . id , DeliveryAddress . delivery_company , DeliveryAddress . delivery_title , DeliveryAddress . delivery_first_name , DeliveryAddress . delivery_last_name , DeliveryAddress . delivery_street , DeliveryAddress . delivery_house_nr , DeliveryAddress . delivery_postal_code , DeliveryAddress . delivery_town , DeliveryAddress . delivery_country , DeliveryAddress . created , DeliveryAddress . deleted , DeliveryAddress . modified , ProductLine . id , ProductLine . name , ProductLine . description , ProductMix1 . id , ProductMix1 . name , ProductMix1 . description , ProductMix1 . image_small_path , ProductMix1 . image_normal_path , ProductMix1 . product_categories_id , ProductMix1 . depricated , ProductMix1 . created , ProductMix1 . modified , ProductMix2 . id , ProductMix2 . name , ProductMix2 . description , ProductMix2 . image_small_path , ProductMix2 . image_normal_path , ProductMix2 . product_categories_id , ProductMix2 . depricated , ProductMix2 . created , ProductMix2 . modified , ProductMix3 . id , ProductMix3 . name , ProductMix3 . description , ProductMix3 . image_small_path , ProductMix3 . image_normal_path , ProductMix3 . product_categories_id , ProductMix3 . depricated , ProductMix3 . created , ProductMix3 . modified , ProductMix4 . id , ProductMix4 . name , ProductMix4 . description , ProductMix4 . image_small_path , ProductMix4 . image_normal_path , ProductMix4 . product_categories_id , ProductMix4 . depricated , ProductMix4 . created , ProductMix4 . modified FROM deliveries AS Delivery LEFT JOIN orders AS Order ON (Delivery . order_id = Order . id) LEFT JOIN delivery_addresses AS DeliveryAddress ON (Delivery . delivery_address_id = DeliveryAddress . id) LEFT JOIN product_lines AS ProductLine ON (Delivery . product_lines_id = ProductLine . id) LEFT JOIN product_mixes AS ProductMix1 ON (Delivery . product_mix_id1 = ProductMix1 . id) LEFT JOIN product_mixes AS ProductMix2 ON (Delivery . product_mix_id2 = ProductMix2 . id) LEFT JOIN product_mixes AS ProductMix3 ON (Delivery . product_mix_id3 = ProductMix3 . id) LEFT JOIN product_mixes AS ProductMix4 ON (Delivery . product_mix_id4 = ProductMix4 . id) WHERE User . id = 1

有人知道爲什麼蛋糕不拉第二層,在這種情況下,用戶模型,甚至遞歸設置爲5時?

非常感謝。

+0

你試過刪除模型緩存(S)? – Ross

回答

0

編輯:它只是發生,我認爲你的情況,你不需要二級JOIN其實,你可以通過Order.user_id(而不是User.id)過濾器!你看到我的觀點了嗎?

所以可能你不需要下面的解決方案。


據我所知,蛋糕從來不做第2級JOIN本身,所以在第二級(和深)過濾(條件)我用joins

對於示例:

$options['joins'] = array(
    array(
     'table' => 'orders', 
     'alias' => 'Order', 
     'type' => 'LEFT', 
     'conditions' => array(
      'Order.id = Delivery.order_id', 
     ) 
    ), 
    array(
     'table' => 'users', 
     'alias' => 'User', 
     'type' => 'LEFT', 
     'conditions' => array(
      'User.id = Order.user_id', 
      'User.some_field' => $someFilteringValue 
     ) 
    ) 
); 
$result = $this->Delivery->find('all', $options);