2013-02-15 38 views
0

我想加入我的查詢,但我無法取得任何成功,我想查詢product表中的productname,如果product_id存在於productplan表中。如何在cakephp中多次加入?

Producttable  
id name 
    1 a 
    2 b 

productplantable 
id product_id 
    1 1 
    2 2 

這是我的查詢:

$p_products = $this->ProductPlan->find('all', 
             array('fields'=> 
             array('ProductPlan.product_id', 
               'Product.name') 
             ) 
            ); 
$this->set('p_products',$p_products); 
+0

您是否定義了各個模型中兩個表之間的關係? – Rikesh 2013-02-15 05:39:20

+0

確定關係中沒有問題,產品hava hasmany關係和產品計劃都屬於與產品的關係 – 2013-02-15 05:42:02

+0

有意義的關係已定義 – 2013-02-15 05:45:21

回答

0

如果你們的關係是正確設置你的ProductPlan模型,下面應該工作。如果沒有,請提供您的ProductPlan模型的源代碼。

$this->ProductPlan->Behaviors->attach('Containable'); 

$p_products = $this->ProductPlan->find('all', array(
    'fields' => array('product_id'), 
    'contain' => array('Product' => array('fields' => array('name'))) 
)); 
$this->set('p_products',$p_products);