How to make relative sample using AR with Yii2?

Hello! For example, I need to output data for categories of news in an associative array, but also the news, but for a specific category.
I.e. the code should be:
$category = 1;
CategoryNews::find()->where(['category' => $category])->with('news')->asArray()->all();


But how to make the news filter through with? In fact, you can set filtering in most respect, but how to convey her argument?
June 10th 19 at 15:54
2 answers
June 10th 19 at 15:56
If the connection is correctly configured, each news is linked to a specific category. If you need additional terms to add, it is possible (example from the docks):
// to find buyers and get them together with their countries and active orders
// SELECT * FROM `customer`
// SELECT * FROM `country` WHERE `id` IN (...)
// SELECT * FROM `order` WHERE `customer_id` IN (...) AND `status` = 1
$customers = Customer::find()->with([
'country',
 'orders' => function ($query) {
 $query->andWhere(['status' => Order::STATUS_ACTIVE]);
},
])->all();
June 10th 19 at 15:58
$category = 1;
CategoryNews::find()
->JoinWith('news')
->where(['category' => $category])
->andWhere([News::tableName() . '.news_field' => 1])
->asArray()
->all();

Find more questions by tags Yii