Cake PHP 3.4 - 如何在同一个查询中添加2个内部联接?

时间:2017-04-07 09:50:14

标签: php mysql cakephp has-and-belongs-to-many

在MAMP上运行PHP 3.4。我有以下情况:

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相关),还有成分。 使用我的代码,我显示的是产品字段(可以),但只显示了连接表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

但我找不到任何可以让我这样查询的东西。 有谁知道如何实现这一目标?

谢谢!

2 个答案:

答案 0 :(得分:0)

我不确定我是否完全遵循您想要实现的目标,但如果您只想要特定产品的所有成分,则contain Ingredients可以获得产品,如下所示: -

$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']);

答案 1 :(得分: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');
    }
}