http://site.com - frontend
http://admin.site.com - backend
class Menu extends ActiveRecord
{
// ...
public static function getList()
{
$data = static::find()
->select(['id', 'parent_id', 'title'])
->orderBy('parent_id ASC')
->asArray()
->all();
$sort = new SortList([
'data' => $data,
'prefix' => '------',
]);
$sortList = ArrayHelper::map($sort->getList(), 'id', 'title');
return $sortList;
}
}
class SortList extends Object
{
public $data;
public $prefix = ' ';
protected function getPath($category_id, $prefix = false)
{
foreach ($this->data as $item) {
if ($category_id == $item['id']) {
$prefix = $prefix ? $this->prefix . $prefix : $item['title'];
if ($item['parent_id']) {
return $this->getPath($item['parent_id'], $prefix);
} else {
return $prefix;
}
}
}
return '';
}
public function getList($parent_id = 0)
{
$data = [];
foreach ($this->data as $item) {
if ($parent_id == $item['parent_id']) {
$data[] = [
'id' => $item['id'],
'title' => $this->getPath($item['id'])
];
$data = array_merge($data, $this->getList($item['id']));
}
}
return $data;
}
}
public function actionCreate()
{
$model = new Menu();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
$data = Product::find()
->joinWith('table')
->where(['table.value_id' => [1, 2, 3]])
->asArray()
->all();
SELECT `product`.* FROM `product` LEFT JOIN `table` ON `product`.`id` = `table`.`product_id` WHERE `table`.`value_id` IN (1, 2, 3);
SELECT * FROM `table` WHERE `product_id` IN ('1', '2');
$data = Product::find()
->with([
'table' => function(Query $q) {
$q->where(['value_id' => [1, 2, 3]]);
}
])
->asArray()
->all();
SELECT * FROM `product`;
SELECT * FROM `table` WHERE (`value_id` IN (1, 2, 3)) AND (`product_id` IN ('1', '2'));
// print_r($data);
Array
(
[0] => Array
(
[id] => 1
[manufacturer_id] => 2
[category_id] => 13
[status] => 10
[name] => фотокамера Canon SX 150 IS black
[created_at] => 1395167522
[updated_at] => 1395167679
[table] => Array
(
[product_id] => 1
[value_id] => 1
)
)
[1] => Array
(
[id] => 2
[manufacturer_id] => 2
[category_id] => 29
[status] => 10
[name] => сумка
[created_at] => 1395170865
[updated_at] => 1395170865
[medium_delivery] => 0
[table] => Array
(
[product_id] => 2
[value_id] => 1
)
)
)