2017-04-07 20 views
0

在MAMP上運行的CakePHP 3.4。我有以下情形:CakePHP 3.4 - 如何在同一個查詢中添加2個內部連接?

SQL表

TABLE `Ingredients` (
    `id` int(11) NOT NULL, 
    `name` varchar(255) NOT NULL, 
    `category_id` int(11) NOT NULL, 
    `measure_id` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 


Table products 
TABLE `Products` (
    `id` int(11) NOT NULL, 
    `name` varchar(255) NOT NULL, 
    `retail_price` float NOT NULL, 
    `best_before` int(11) NOT NULL, 
    `comments` text NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 



TABLE `ingredients_products` (
    `ingredient_id` int(11) NOT NULL, 
    `product_id` int(11) NOT NULL, 
    `qty` double NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

表型號:

class IngredientsTable extends Table 
{ 

    public function initialize(array $config) 
    { 
     parent::initialize($config); 

     $this->setTable('ingredients'); 
     $this->setDisplayField('name'); 
     $this->setPrimaryKey('id'); 

     $this->hasMany('Purchases', [ 
      'foreignKey' => 'ingredient_id' 
     ]); 
     $this->hasMany('IngredientsProducts', [ 
      'foreignKey' => 'ingredient_id' 
     ]); 
    } 

class ProductsTable extends Table 
{ 

    public function initialize(array $config) 
    { 
     parent::initialize($config); 

     $this->setTable('products'); 
     $this->setDisplayField('name'); 
     $this->setPrimaryKey('id'); 

     $this->hasMany('IngredientsProducts', [ 
      'foreignKey' => 'product_id' 
     ]); 
    } 

介紹CakePHP,我跟着cookbok的書籤用教程(適用於我的情況下)。該邏輯將應該有一個產品有很多成分。烘烤後,這似乎工作正常。

我想達到的目標是:在特定產品的產品視圖上,我要顯示產品字段(與產品ID相關),以及成分。 用我的代碼,因爲它顯示產品字段(這是可以的),但只有joiner表ingredients_products的相關ID。

public function view($id = null) 
    { 
     $product = $this->Products->get($id, [ 
      'contain' => ['IngredientsProducts'] 
     ]); 

     $this->set('product', $product); 
     $this->set('_serialize', ['product']); 



    } 

在SQL我將運行查詢:

SELECT products.name as prod, ingredients.name as ingr, ingredients_products.qty as qty 
FROM ingredients_products 
    INNER JOIN products 
     ON ingredients_products.product_id = products.id 
    INNER JOIN ingredients 
     ON ingredients_products.ingredient_id = Ingredients.id 

我一直在努力的事情,我查詢生成器頁面上找到:https://book.cakephp.org/3.0/en/orm/query-builder.html

,但我無法找到任何東西,會讓我做這樣的查詢。 有誰知道這是如何實現的?

謝謝!

回答

0

這聽起來很像一個屬於,有許多關係。

class IngredientsTable extends Table 
{ 
    public function initialize(array $config) 
    { 
     // Use through option because it looks like you 
     // have additional data on your IngredientsProducts table 
     $this->belongsToMany('Products', [ 
      'through' => 'IngredientsProducts', 
     ]); 
    } 
} 

class ProductsTable extends Table 
{ 
    public function initialize(array $config) 
    { 
     $this->belongsToMany('Ingredients', [ 
      'through' => 'IngredientsProducts', 
     ]); 
    } 
} 

class IngredientsProductsTable extends Table 
{ 
    public function initialize(array $config) 
    { 
     $this->belongsTo('Ingredients'); 
     $this->belongsTo('Products'); 
    } 
} 
+0

這個伎倆。謝謝!我已閱讀有關belongsToMany,但不知道如何在此示例上實現它。 – Tzi

0

我不知道我完全跟着你想要達到的目標,但如果你只是想針對特定產品的所有成分,你可以containIngredients像這樣的產品: -

$product = $this->Products->get($id, [ 
    'contain' => ['Ingredients'] 
]); 

然而,如果你想實現你的問題中描述的SQL查詢,那麼你可以爲連接表定義模型,然後查詢它。所以,你就會有一個IngredientsProductsTable模型: -

class IngredientsProductsTable extends Table 
{ 
    public function initialize(array $config) 
    { 
     $this->belongsTo('Ingredients'); 
     $this->belongsTo('Products'); 
    } 
} 

然後在你的控制器: -

$this->loadModel('IngredientsProducts'); 
$data = $this->IngredientsProducts->find('all') 
    ->contain(['Ingredients', 'Products']); 
+0

嗨!謝謝您的回答。對不起,如果我不清楚我的問題。我想要做的是,當我打開特定產品的視圖時,在該視圖中,我想按照joiner表ingredients_products顯示與該產品相關的所有成分。我試着定義了模型IngredientsProductsTable,現在我得到了所有的產品。我如何修改$ this-> loadModel('IngredientsProducts'); $ data = $ this-> IngredientsProducts-> find('all') - > contains(['Ingredients','Products']);尋找一個特定的產品ID?謝謝! – Tzi

+0

在這種情況下,您只需使用我的第一個示例和「包含」成分。這將使用連接表來檢索返回產品的相關成分。 – drmonkeyninja

+0

太棒了!但奇怪的是,當使用第一個例子時,我得到了「產品與成分無關」的錯誤。 – Tzi

相關問題