cakephp3查找所选列的数据

时间:2016-04-08 09:38:43

标签: mysql cakephp cakephp-3.0 cakephp-3.1

您好,我正试图在Cakephp3中找到数据

我正在做的是我想查找包含产品名称,产品代码和小图片网址的所选列的数据。

$contain = [
    'Image' => [
            'ImageFile'
        ]
    ];

$products = $this->Product->find()
        ->contain($contain)
        ->select(['Product.name', 'Product.product_code', 'Product.Image.ImageFile.small_image_url'])
        ->where(['Product.name Like' => '%'.$keyword.'%']);

但是,我收到了错误

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Product.Image.ImageFile.small_image_url' in 'field list'

我不知道如何解决这个问题。

提前致谢!

1 个答案:

答案 0 :(得分:0)

首先,您能否告诉我们DB的结构是什么?

其次,有一种方法可以通过调试工具包调试cake3中的查询。请告诉我们查询的结果:

$products = $this->Product->find()
        ->contain($contain)
        ->select(['Product.name', 'Product.product_code', 'Product.Image.ImageFile.small_image_url'])
        ->where(['Product.name Like' => '%'.$keyword.'%']);

请尝试此查询并告诉我调试工具包中的输出:

$contain=[
    ['Image' => function($q) {
        return $q->autoFields(true);
    },
    'ImageFile'=> function($q) {
        return $q->autoFields(true);
    }
    ],
];
$products = $this->Product->find()
                 ->contain($contain)
                 ->autoFields(true)          
                 ->where(['Product.name Like' => '%'.$keyword.'%']);