【问题标题】:Cakephp 2.8 pulling data from three tables with one common column per pair tableCakephp 2.8 从三个表中提取数据,每对表一个公共列
【发布时间】:2019-05-26 22:26:08
【问题描述】:

我有三个表,我想知道什么是正确的 Find 命令,可以根据所选类别提取正确的汽车型号。

Category table
---------------------
Cat_ID Category
1      Sedan
2      SUV
3      Truck

Manufacturers tables
--------------------------
CAR_ID  Cat_ID   Manufacturer
1        1          BMW
2        1          BMW
3        2         Mercedes
4        3          Dodge

Model table
--------------
CAR_ID   Model
1        i320
2        i540
3        GL320
4        RAM

我能够根据制造商拉出正确的汽车,但是当我尝试根据类别拉出汽车时它不起作用。

$catid='Sedan';

$car_id = $this->car->find('all',array('fields' => array('car.id'),'conditions' => array('car.category_id' => $catid),'order' => array('car.id' => 'desc')));

$models = $this->Model->find('all',array('conditions' => array('Model.car_id'=>$car_id),'order' => array('Model.id' => 'desc')));

如果我选择轿车,我希望输出显示宝马 i32 和 i520 车型。

【问题讨论】:

  • CakePHP 2.x 或 3.x 版?
  • 蛋糕php 2.8版

标签: cakephp


【解决方案1】:

使用连接语句

我不知道您使用的是什么版本的 cakephp,但我使用的是 cakephp 2.0。

        $this->Model->find('all',[
        'joins' => [
            [
                'table' => 'Manufacturers',
                'alias' => 'Manufacturers',
                'type' => 'LEFT',
                'conditions' => [
                    'Category.Cat_ID = Manufacturers.Cat_ID'
                ]
            ],
            [
                'table' => 'Model',
                'type' => 'LEFT',
                'conditions' => [
                    'Model.CAR_ID = Manufacturers.CAR_ID'
                ]
            ]
        ]
      ]);

如果您想限制将使用的类别,请使用如下条件

    $this->Model->find('all',[
        'joins' => [
            [
                'table' => 'Manufacturers',
                'alias' => 'Manufacturers',
                'type' => 'LEFT',
                'conditions' => [
                    'Category.Cat_ID = Manufacturers.Cat_ID'
                ]
            ],
            [
                'table' => 'Model',
                'type' => 'LEFT',
                'conditions' => [
                    'Model.CAR_ID = Manufacturers.CAR_ID'
                ]
            ]
        ],
        'conditions' => [
            'Category.Cat_ID' => 1
        ]
    ]);

旁注:使用 '[]' 和 array() 在技术上是相同的,但是,这里是使用 array() 的示例代码。

    $this->Category->find('all', 
    array(
        'joins' => array(
            array(
                'table' => 'Manufacturers',
                'alias' => 'Manufacturers',
                'type' => 'LEFT',
                'conditions' => array(
                    'Category.Cat_ID = Manufacturers.Cat_ID'
                )
            ),
            array(
                'table' => 'Model',
                'type' => 'LEFT',
                'conditions' => array(
                    'Model.CAR_ID = Manufacturers.CAR_ID'
                )
            ),
        ),
        'conditions' => array(
            'Category.Cat_ID' => 1
        )
    ));

更新 2

        $this->Model->find('all', 
        array(
            'joins' => array(
                array(
                    'table' => 'Manufacturers',
                    'alias' => 'Manufacturers',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Model.Car_ID = Manufacturers.Car_ID '
                    )
                ),
                array(
                    'table' => 'Category',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Category.Cat_ID= Manufacturers.Cat_ID'
                    )
                ),
            ),
            'conditions' => array(
                'Category.Cat_ID'=>$cat_id
            )
        ));

【讨论】:

  • 谢谢阿尔伯特。我认为您缺少数组(),您可以将数组添加到您的答案中。
  • 基本上 array() 和 [] 在技术上是相同的。在 cakephp 中编程时,您可以使用其中任何一个。无论如何,我已经更新了我的答案以满足您的问题:D
  • 谢谢 Albert,我还是 cakephp 的初学者。我需要输出是模型匹配的记录,我得到的是整个类别表。你能验证你的代码吗我的朋友。
  • 只需将 'type' => 'LEFT' 替换为 'type' => 'INNER',
  • 仍然是同样的问题,当我执行 dump_var 时,我得到的类别表与 Sedan 类别匹配很多次。
猜你喜欢
  • 2022-12-08
  • 2016-03-25
  • 2020-01-23
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-02
相关资源
最近更新 更多