2

您好我正在尝试在 Cakephp3 中查找数据

我正在做的是我想查找具有选定列的数据,这些列是产品名称、产品代码和小图像 url。

$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'

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

提前致谢!

4

1 回答 1

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.'%']);
于 2016-04-08T13:09:07.847 回答