1
votes

I want to use category listing in subcategory controller. I am trying following methods to get categories which status are 1.

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

This is returning following output

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

I don't know where it is getting:c0. Then I used another code which is giving all the list but where clause is not working.

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

The output is

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

Please suggest a suitable way.

2
Does it not make more sense to put the ->where() before running the ->find()RiggsFolly
"doesn't work" is not a proper problem description! Even if the problem might be obvious for people that know the CakePHP internals, please always be as specific as possible as to what exactly happens, and what exactly you'd expect to happen instead. Show the data that you are working with, the context, the code required to reproduce the problem, your debugging attempts, and possible errors. That being said, what you're showing there is a totally valid SQL query with a placeholder for a bound value.ndm
@RiggsFolly In CakePHP 3.x, find() returns a query (builder) object.ndm
@ndm Ah that explains the :c0 which would be used as a Bound ParameterRiggsFolly

2 Answers

0
votes

Pass conditions with query like below example.

Example

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

You need to add ->first() to your where clause, like so :

$query->where(['cat_status' => 1])->first();