Created
January 31, 2018 22:58
-
-
Save andku83/f6ca24c66693d185eacf638c3b6b1a18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace common\models\query; | |
use common\models\Category; | |
use yii\db\ActiveQuery; | |
/** | |
* This is the ActiveQuery class for [[Category]]. | |
* | |
* @see Category | |
*/ | |
class CategoryQuery extends ActiveQuery | |
{ | |
/** | |
* @var | |
*/ | |
protected $_data; | |
/** | |
* @return $this | |
*/ | |
public function active() | |
{ | |
return $this->andWhere(['{{%category}}.status' => Category::STATUS_ACTIVE]); | |
} | |
/** | |
* @param $slug | |
* @return $this | |
*/ | |
public function bySlug($slug) | |
{ | |
return $this->andWhere(['{{%category}}.slug' => $slug]); | |
} | |
/** | |
* @param $id | |
* @return $this | |
*/ | |
public function byMenuId($id) | |
{ | |
$this->joinWith('categoryMenus categoryMenus', false) | |
->andWhere(['categoryMenus.menu_id' => $id]) | |
; | |
return $this; | |
} | |
/** | |
* @return $this | |
*/ | |
public function onlyMain() | |
{ | |
return $this->andWhere('{{%category}}.parent_id is NULL'); | |
} | |
/** | |
* @param int $exception_id | |
* @return array | |
*/ | |
public function getList($exception_id = 0) | |
{ | |
$this->select(['name', 'id'])->indexBy('id'); | |
if ($exception_id){ | |
$this->andWhere(['!=', 'id', $exception_id]); | |
} | |
return $this->column(); | |
} | |
/** | |
* @param array $fields | |
* | |
* @return array | |
*/ | |
public function getAllAsTree($fields = ['name', 'id', 'slug']) | |
{ | |
$this->select(array_merge($fields,['parent_id'])) | |
->addOrderBy(['lvl' => SORT_ASC, 'sorting' => SORT_ASC, 'id' => SORT_ASC]) | |
->indexBy('id'); | |
$this->_data = $this->asArray()->all(); | |
$result = $this->createTree(); | |
return $result; | |
} | |
/** | |
* @param array $ids | |
* | |
* @return array | |
*/ | |
public function getChildrenIds($ids = []) | |
{ | |
return $this->innerJoinWith('childs childs', false) | |
->select('childs.id') | |
->andWhere(['{{%category}}.id' => $ids, 'childs.status' => Category::STATUS_ACTIVE]) | |
->andWhere(['NOT LIKE','{{%childs}}.name', 'аксессуары']) | |
->column(); | |
} | |
/** | |
* @return array | |
*/ | |
public function getListForSelectAsTree() | |
{ | |
$tree = $this->getAllAsTree(); | |
return $this->createTreeSelect($tree); | |
} | |
/** | |
* @return array | |
*/ | |
public function getParentsList() | |
{ | |
// Выбираем только те категории, у которых есть дочерние категории | |
$this->select(['id' => 'parent.id', 'name' => 'parent.name', 'parent_id' => 'parent.parent_id']) | |
->innerJoinWith('parent parent', false) | |
->andWhere(['parent.status' => Category::STATUS_ACTIVE]) | |
->orderBy(['parent.lvl'=>SORT_ASC, 'parent.sorting'=>SORT_ASC, 'parent.id' => SORT_ASC]) | |
->indexBy('id') | |
->distinct(); | |
$this->_data = $this->asArray()->all(); | |
$tree = $this->createTree(); | |
$list = $this->createTreeSelect($tree); | |
return $list; | |
} | |
/** | |
* @return array | |
*/ | |
protected function createTree() | |
{ | |
$tree = []; | |
foreach ($this->_data as $key => &$item) { | |
if (!$item['parent_id']){ | |
$tree[$item['id']] = &$item; | |
} elseif(!empty($this->_data[$item['parent_id']])) { | |
$this->_data[$item['parent_id']]['items'][$item['id']] = &$item; | |
} | |
} | |
return $tree; | |
} | |
/** | |
* @param array $data | |
* @param string $prefix | |
* @return array | |
*/ | |
protected function createTreeSelect($data = array(), $prefix = '') | |
{ | |
$list = []; | |
foreach ($data as $item) { | |
$list[$item['id']] = $prefix.$item['name']; | |
if (!empty($item['items'])) { | |
$list = $list + $this->createTreeSelect($item['items'], ($prefix) . $item['name'] . '->' ); | |
} | |
} | |
return $list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment