W
W
warcevil2016-12-01 13:13:41
Yii
warcevil, 2016-12-01 13:13:41

How to add custom field to ActiveRecord in YII2?

Can you tell me how to add a custom field to an object? I have menu items that have products attached to them. So the task is to display a specific menu with goods. I do this:
public function getMenusWithProducts($limitProducts)
{
$menu = $this->find()->all();
$products = [];
foreach ($menu as $item) {
$products[] = Products::find()
->where(['MenuID' => $item->MenuID])
->limit($limitProducts)
->all();
}
$items = [];
foreach ($menu as $item) {
if (!empty($products)) {
$item->products = $products;
$items[] = $item;
}
$menu = $items;
return $menu;
}
Но какжется это слишком глупо.. К тому же не работает как надо ) Как вы бы сделали?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Максим Федоров, 2016-12-01
@qonand

используйте для решения этой задачи встроенные механизмы связей (реляций)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question