S
S
sdgs4s4 .2018-03-07 21:10:07
Yii
sdgs4s4 ., 2018-03-07 21:10:07

Yii2 -> how to get category id in controller?

There is a controller:

$categorys = Category::find()->where(['parent_id' => $id, 'status' => '1'])->all();
        
        $categorys1 = $categorys->id;
        
        $query = Product::find()->where(['category_id' => $id, 'status' => '1'])->andWhere(['category_id' => $categorys1])->addOrderBy('id DESC');

The problem is $categorys1 = $categorys->id;
If I put $categorys1 = ['38', '43']- 38 and 43 - this is the id that should be received from the request to the model Category
As a result, these id (38 and 43) should come in andWhere(['category_id' => $categorys1])
If there are 38 and 43 - the function is executed, the goods are retrieved from the database, and if I try to get through
$categorys = Category::find()->where(['parent_id' => $id, 'status' => '1'])->all();
- does not give
Help!!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2018-03-07
@Encoderast

1. all() must return an array, and you treat it as an object?
2. you make a request to the products "category_id = $id AND category_id = $id2", which of course will not return anything.
3. it turns out you need to do IN (), somehow it was like this:

->where(['status' => '1'])->andWhere(['in', 'category_id', $categoryIds])

$categoryIds - array of required category id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question