php - where 子句在 cakephp 3.4 中不起作用

标签 php mysql cakephp cakephp-3.0

我想在子类别 Controller 中使用类别列表。我正在尝试使用以下方法来获取状态为 1 的类别。

$this->loadModel('Categories');
$query = $this->Categories->find();
$query->where(['cat_status' => 1]);

返回以下输出

'SELECT Categories.id AS Categories__id, Categories.cat_name AS Categories__cat_name, Categories.cat_status AS Categories__cat_status FROM categories Categories WHERE cat_status = :c0'

我不知道它从哪里得到:c0。然后我使用了另一个代码,它给出了所有列表,但 where 子句不起作用。

$this->loadModel('Categories');
$categories = $this->Categories->find('all',[
                                            'where' => (['cat_status'=>1]),
                                            'limit' => 5,
                                            'order' => 'Categories.id DESC'
    ]);
debug($categories);

输出是

SELECT Categories.id AS Categories__id, Categories.cat_name AS Categories__cat_name, Categories.cat_status AS Categories__cat_status FROM categories Categories ORDER BY Categories.id DESC LIMIT 5

请提出合适的方法。

最佳答案

通过查询传递条件,如下例所示。

例子

$categories = $this->Categories->find('all',['conditions'=>['cat_status'=>1]])->limit(5)->order(['Categories.id'=> 'DESC']);

关于php - where 子句在 cakephp 3.4 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44885412/

相关文章:

Mysql在非空字段上插入或更新为空值时不会出错

mysql - 我如何最好地避免在 CakePHP 中插入重复记录?

cakephp - blackhole cakephp 2 关联实体

json - Cakephp - 如果请求是 JSON?

mysql - CakePHP 3.x - 跨多个表保存关联数据

php - 如何将变量添加到数组?

PHP 加载数据 INFILE VS Fopen

mysql - 限制两个不同 SQL 查询的结果

php - SQL 总计百分比计算

php - 仅在生产中空左连接