Commit 14eadb86e48260b0638ba6bc9cad2e473cdf3173

Authored by Karnovsky A
1 parent 290fae5b

Eager loading for catalog-models and autocomplete for relation create/update

backend/controllers/CategoryController.php
@@ -90,7 +90,7 @@ class CategoryController extends Controller @@ -90,7 +90,7 @@ class CategoryController extends Controller
90 } 90 }
91 return $this->render('create', [ 91 return $this->render('create', [
92 'model' => $model, 92 'model' => $model,
93 - 'categories' => ArtboxTreeHelper::treeMap(Category::find()->getTree(), 'category_id', 'name', '.') 93 + 'categories' => ArtboxTreeHelper::treeMap(Category::find()->with('categoryName')->getTree(), 'category_id', 'categoryName.value', '.')
94 ]); 94 ]);
95 } 95 }
96 } 96 }
@@ -110,7 +110,7 @@ class CategoryController extends Controller @@ -110,7 +110,7 @@ class CategoryController extends Controller
110 } else { 110 } else {
111 return $this->render('update', [ 111 return $this->render('update', [
112 'model' => $model, 112 'model' => $model,
113 - 'categories' => ArtboxTreeHelper::treeMap(Category::find()->getTree(), 'category_id', 'name', '.') 113 + 'categories' => ArtboxTreeHelper::treeMap(Category::find()->with('categoryName')->getTree(), 'category_id', 'categoryName.value', '.')
114 ]); 114 ]);
115 } 115 }
116 } 116 }
common/components/artboxtree/ArtboxTreeBehavior.php
@@ -89,19 +89,20 @@ class ArtboxTreeBehavior extends Behavior { @@ -89,19 +89,20 @@ class ArtboxTreeBehavior extends Behavior {
89 * get all-level children items 89 * get all-level children items
90 * use MP-method 90 * use MP-method
91 */ 91 */
92 - public function getAllChildren($depth = null, $where = []) {  
93 - return $this->getAllChildrenMP($depth)->where($where); 92 + public function getAllChildren($depth = null, $where = [], $with = null) {
  93 + return $this->getAllChildrenMP($depth, $where, $with);
94 } 94 }
95 95
96 /* 96 /*
97 * get all-level children items 97 * get all-level children items
98 * use MP-method 98 * use MP-method
99 */ 99 */
100 - public function getAllChildrenTree($depth = null, $where = []) {  
101 - return $this->buildTree($this->getAllChildrenMP($depth, $where)->all(), $this->owner->getAttribute($this->keyNameId)); 100 + public function getAllChildrenTree($depth = null, $where = [], $with = null) {
  101 + $query = $this->getAllChildrenMP($depth, $where, $with);
  102 + return $this->buildTree($query->all(), $this->owner->getAttribute($this->keyNameId));
102 } 103 }
103 104
104 - protected function buildTree(array $data, $parentId = 0) { 105 + public function buildTree(array $data, $parentId = 0) {
105 $result = []; 106 $result = [];
106 foreach ($data as $key => $element) { 107 foreach ($data as $key => $element) {
107 if ($element->getAttribute($this->keyNameParentId) == $parentId) { 108 if ($element->getAttribute($this->keyNameParentId) == $parentId) {
@@ -205,7 +206,7 @@ class ArtboxTreeBehavior extends Behavior { @@ -205,7 +206,7 @@ class ArtboxTreeBehavior extends Behavior {
205 } 206 }
206 207
207 208
208 - public function getAllChildrenMP($depth = null) 209 + public function getAllChildrenMP($depth = null, $where = [], $with = null)
209 { 210 {
210 $tableName = $this->owner->tableName(); 211 $tableName = $this->owner->tableName();
211 $path = $this->owner->getAttribute($this->keyNamePath); 212 $path = $this->owner->getAttribute($this->keyNamePath);
@@ -220,6 +221,13 @@ class ArtboxTreeBehavior extends Behavior { @@ -220,6 +221,13 @@ class ArtboxTreeBehavior extends Behavior {
220 $orderBy["{$tableName}.[[{$this->keyNameDepth}]]"] = SORT_ASC; 221 $orderBy["{$tableName}.[[{$this->keyNameDepth}]]"] = SORT_ASC;
221 $orderBy["{$tableName}.[[{$this->keyNameId}]]"] = SORT_ASC; 222 $orderBy["{$tableName}.[[{$this->keyNameId}]]"] = SORT_ASC;
222 223
  224 + if ($where) {
  225 + $query->andWhere($where);
  226 + }
  227 + if ($with) {
  228 + $query->with($with);
  229 + }
  230 +
223 $query 231 $query
224 ->andWhere($this->groupWhere()) 232 ->andWhere($this->groupWhere())
225 ->addOrderBy($orderBy); 233 ->addOrderBy($orderBy);
common/components/artboxtree/ArtboxTreeQueryTrait.php
@@ -23,13 +23,15 @@ trait ArtboxTreeQueryTrait { @@ -23,13 +23,15 @@ trait ArtboxTreeQueryTrait {
23 return self::$model; 23 return self::$model;
24 } 24 }
25 25
26 - public function getTree($group = null) { 26 + public function getTree($group = null, $with = null) {
27 $model = $this->getModel(); 27 $model = $this->getModel();
28 if ($group !== null) { 28 if ($group !== null) {
29 - $data = $this->andWhere([$model->keyNameGroup => $group])->all();  
30 - } else {  
31 - $data = $this->all(); 29 + $this->andWhere([$model->keyNameGroup => $group]);
32 } 30 }
  31 + if ($with) {
  32 + $this->with($with);
  33 + }
  34 + $data = $this->all();
33 if (empty($data)) 35 if (empty($data))
34 return []; 36 return [];
35 37
@@ -53,8 +55,8 @@ trait ArtboxTreeQueryTrait { @@ -53,8 +55,8 @@ trait ArtboxTreeQueryTrait {
53 /** 55 /**
54 * @param int $group 56 * @param int $group
55 */ 57 */
56 - public function rebuildMP($group) {  
57 - $tree = $this->getTree($group); 58 + public function rebuildMP($group, $with = null) {
  59 + $tree = $this->getTree($group, $with);
58 60
59 $this->_recursiveRebuild($tree); 61 $this->_recursiveRebuild($tree);
60 } 62 }
common/config/main.php
@@ -81,14 +81,17 @@ return [ @@ -81,14 +81,17 @@ return [
81 'entity1' => [ 81 'entity1' => [
82 'model' => '\common\modules\product\models\Product', 82 'model' => '\common\modules\product\models\Product',
83 'label' => 'Product', 83 'label' => 'Product',
84 - 'listField' => 'fullname', 84 + 'listField' => 'name',
  85 + 'searchField' => 'name',
85 'key' => 'product_id', 86 'key' => 'product_id',
86 'linked_key' => 'product_id', 87 'linked_key' => 'product_id',
87 ], 88 ],
88 'entity2' => [ 89 'entity2' => [
89 'model' => '\common\modules\product\models\Category', 90 'model' => '\common\modules\product\models\Category',
90 'label' => 'Category', 91 'label' => 'Category',
91 - 'listField' => 'name', 92 + 'listField' => 'categoryName.value',
  93 + 'searchField' => 'category_name.value',
  94 + 'searchJoin' => 'categoryName',
92 'key' => 'category_id', 95 'key' => 'category_id',
93 'linked_key' => 'category_id', 96 'linked_key' => 'category_id',
94 'hierarchy' => [ 97 'hierarchy' => [
common/modules/product/helpers/ProductHelper.php
@@ -8,10 +8,10 @@ use yii\base\Object; @@ -8,10 +8,10 @@ use yii\base\Object;
8 8
9 class ProductHelper extends Object { 9 class ProductHelper extends Object {
10 public static function getCategories() { 10 public static function getCategories() {
11 - return Category::find()->getTree(); 11 + return Category::find()->with('categoryName')->getTree();
12 } 12 }
13 13
14 public static function getBrands() { 14 public static function getBrands() {
15 - return Brand::find(); 15 + return Brand::find()->with('brandName');
16 } 16 }
17 } 17 }
18 \ No newline at end of file 18 \ No newline at end of file
common/modules/product/models/Brand.php
@@ -111,6 +111,7 @@ class Brand extends \yii\db\ActiveRecord @@ -111,6 +111,7 @@ class Brand extends \yii\db\ActiveRecord
111 } 111 }
112 112
113 public function getName() { 113 public function getName() {
  114 + return $this->brandName->value;
114 $value = $this->getBrandName()->one(); 115 $value = $this->getBrandName()->one();
115 return empty($value) ? $this->_getValue('name') : $value->value; 116 return empty($value) ? $this->_getValue('name') : $value->value;
116 } 117 }
common/modules/product/models/BrandSearch.php
@@ -41,7 +41,7 @@ class BrandSearch extends Brand @@ -41,7 +41,7 @@ class BrandSearch extends Brand
41 */ 41 */
42 public function search($params) 42 public function search($params)
43 { 43 {
44 - $query = Brand::find(); 44 + $query = Brand::find()->with('brandName');
45 45
46 // add conditions that should always apply here 46 // add conditions that should always apply here
47 47
common/modules/product/models/Category.php
@@ -87,6 +87,7 @@ class Category extends \yii\db\ActiveRecord @@ -87,6 +87,7 @@ class Category extends \yii\db\ActiveRecord
87 [['alias', 'name'], 'string', 'max' => 250], 87 [['alias', 'name'], 'string', 'max' => 250],
88 [['populary'], 'boolean'], 88 [['populary'], 'boolean'],
89 [['group_to_category'], 'safe'], 89 [['group_to_category'], 'safe'],
  90 + [['category_name_id'], 'exist', 'skipOnError' => true, 'targetClass' => CategoryName::className(), 'targetAttribute' => ['category_name_id' => 'category_name_id']],
90 // [['image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif'], 91 // [['image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif'],
91 // [['product_unit_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductUnit::className(), 'targetAttribute' => ['product_unit_id' => 'product_unit_id']], 92 // [['product_unit_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductUnit::className(), 'targetAttribute' => ['product_unit_id' => 'product_unit_id']],
92 ]; 93 ];
@@ -160,8 +161,9 @@ class Category extends \yii\db\ActiveRecord @@ -160,8 +161,9 @@ class Category extends \yii\db\ActiveRecord
160 return $this->hasOne(CategoryName::className(), ['category_name_id' => 'category_name_id']); 161 return $this->hasOne(CategoryName::className(), ['category_name_id' => 'category_name_id']);
161 } 162 }
162 163
163 - public function getName() {  
164 - $value = $this->getCategoryName()->one();  
165 - return empty($value) ? $this->_getValue('name') : $value->value;  
166 - } 164 +// public function getName() {
  165 +//// return $this->getCategoryName();
  166 +// $value = $this->getCategoryName()->one();
  167 +// return empty($value) ? $this->_getValue('name') : $value->value;
  168 +// }
167 } 169 }
common/modules/product/models/CategoryQuery.php
@@ -34,15 +34,4 @@ class CategoryQuery extends \yii\db\ActiveQuery @@ -34,15 +34,4 @@ class CategoryQuery extends \yii\db\ActiveQuery
34 { 34 {
35 return parent::one($db); 35 return parent::one($db);
36 } 36 }
37 -  
38 - /**  
39 - * Select category by alias  
40 - * @param $slug  
41 - * @return $this  
42 - */  
43 - public function byAlias($alias)  
44 - {  
45 - $this->andFilterWhere(['alias' => $alias]);  
46 - return $this;  
47 - }  
48 } 37 }
common/modules/product/models/CategorySearch.php
@@ -55,7 +55,7 @@ class CategorySearch extends Category @@ -55,7 +55,7 @@ class CategorySearch extends Category
55 */ 55 */
56 public function search($params) 56 public function search($params)
57 { 57 {
58 - $query = Category::find(); 58 + $query = Category::find()->with('categoryName');
59 59
60 // add conditions that should always apply here 60 // add conditions that should always apply here
61 61
@@ -88,8 +88,9 @@ class CategorySearch extends Category @@ -88,8 +88,9 @@ class CategorySearch extends Category
88 88
89 public static function findByAlias($alias) { 89 public static function findByAlias($alias) {
90 /** @var CategoryQuery $query */ 90 /** @var CategoryQuery $query */
91 - $query = Category::find();  
92 - $query->byAlias($alias); 91 + $query = Category::find()
  92 + ->with('categoryName')
  93 + ->andFilterWhere(['alias' => $alias]);
93 if (($model = $query->one()) !== null) { 94 if (($model = $query->one()) !== null) {
94 return $model; 95 return $model;
95 } else { 96 } else {
common/modules/product/models/Product.php
@@ -22,7 +22,7 @@ use yii\db\ActiveQuery; @@ -22,7 +22,7 @@ use yii\db\ActiveQuery;
22 */ 22 */
23 class Product extends \yii\db\ActiveRecord 23 class Product extends \yii\db\ActiveRecord
24 { 24 {
25 - /** @var array $variants */ 25 + /** @var array $_variants */
26 public $_variants = []; 26 public $_variants = [];
27 27
28 /** @var array $_images */ 28 /** @var array $_images */
common/modules/product/widgets/brandsCarouselWidget.php
@@ -13,7 +13,7 @@ class brandsCarouselWidget extends Widget { @@ -13,7 +13,7 @@ class brandsCarouselWidget extends Widget {
13 } 13 }
14 14
15 public function run() { 15 public function run() {
16 - $brands = Brand::find()->all(); 16 + $brands = Brand::find()->with('brandName')->all();
17 return $this->render('brandsCarousel', [ 17 return $this->render('brandsCarousel', [
18 'brands' => $brands, 18 'brands' => $brands,
19 ]); 19 ]);
common/modules/product/widgets/catalogSubmenuWidget.php
@@ -15,12 +15,22 @@ class catalogSubmenuWidget extends Widget { @@ -15,12 +15,22 @@ class catalogSubmenuWidget extends Widget {
15 } 15 }
16 16
17 public function run() { 17 public function run() {
  18 + /** @var Category $rootCategory */
18 $rootCategory = Category::findOne($this->root_id); 19 $rootCategory = Category::findOne($this->root_id);
  20 +
  21 + $categories = $rootCategory->getAllChildren(2, [], 'categoryName')->all();
  22 + $populary = [];
  23 + foreach($categories as $category) {
  24 + if ($category->populary) {
  25 + $populary[] = $category;
  26 + }
  27 + }
  28 +
19 return $this->render('submenu', [ 29 return $this->render('submenu', [
20 'rootCategory' => $rootCategory, 30 'rootCategory' => $rootCategory,
21 'rootClass' => $this->rootClass, 31 'rootClass' => $this->rootClass,
22 - 'populary' => $rootCategory->getAllChildren(2, ['populary' => true])->all(),  
23 - 'items' => $rootCategory->getAllChildrenTree(2) 32 + 'populary' => $populary,
  33 + 'items' => $rootCategory->buildTree($categories, $rootCategory->category_id)
24 ]); 34 ]);
25 } 35 }
26 } 36 }
27 \ No newline at end of file 37 \ No newline at end of file
common/modules/product/widgets/views/submenu.php
1 <div class="menu_item"> 1 <div class="menu_item">
2 - <?= \yii\helpers\Html::a($rootCategory->name, ['catalog/category', 'alias' => $rootCategory->alias], ['class' => 'submenu_button '. $rootClass])?> 2 + <?= \yii\helpers\Html::a($rootCategory->categoryName->value, ['catalog/category', 'alias' => $rootCategory->alias], ['class' => 'submenu_button '. $rootClass])?>
3 <div class="submenu"> 3 <div class="submenu">
4 <ul class="categories"> 4 <ul class="categories">
5 <li class="sub_cat"><span>Популярные категории</span> 5 <li class="sub_cat"><span>Популярные категории</span>
@@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
15 <img valign="top" src="<?= $_item->image?>"> 15 <img valign="top" src="<?= $_item->image?>">
16 <?php endif?> 16 <?php endif?>
17 </div> 17 </div>
18 - <div class="title"><?= $_item->name?></div> 18 + <div class="title"><?= $_item->categoryName->value?></div>
19 </a></div> 19 </a></div>
20 <?php endforeach?> 20 <?php endforeach?>
21 </div> 21 </div>
@@ -24,7 +24,7 @@ @@ -24,7 +24,7 @@
24 </li> 24 </li>
25 <?php foreach($items as $item) :?> 25 <?php foreach($items as $item) :?>
26 <li class="sub_cat"> 26 <li class="sub_cat">
27 - <span><?= $item['item']->name?></span> 27 + <span><?= $item['item']->categoryName->value?></span>
28 <?php if(!empty($item['children'])) :?> 28 <?php if(!empty($item['children'])) :?>
29 <div class="sub_cat_content"> 29 <div class="sub_cat_content">
30 <div class="content_items"> 30 <div class="content_items">
@@ -34,17 +34,17 @@ @@ -34,17 +34,17 @@
34 <?php if (empty($_item['item']->image)) :?> 34 <?php if (empty($_item['item']->image)) :?>
35 <img valign="top" src="/images/no_photo.png"> 35 <img valign="top" src="/images/no_photo.png">
36 <?php else :?> 36 <?php else :?>
37 - <img valign="top" src="<?= $_item['item']->image?>" alt="<?= $_item['item']->name?>"> 37 + <img valign="top" src="<?= $_item['item']->image?>" alt="<?= $_item['item']->categoryName->value?>">
38 <?php endif?> 38 <?php endif?>
39 </div> 39 </div>
40 - <div class="title"><?= $_item['item']->name?></div> 40 + <div class="title"><?= $_item['item']->categoryName->value?></div>
41 </a></div> 41 </a></div>
42 <?php endforeach?> 42 <?php endforeach?>
43 </div> 43 </div>
44 </div> 44 </div>
45 <?php endif?> 45 <?php endif?>
46 </li> 46 </li>
47 - <?php endforeach?> 47 + <?php endforeach ?>
48 </ul> 48 </ul>
49 </div> 49 </div>
50 </div> 50 </div>
common/modules/relation/controllers/ManageController.php
@@ -4,8 +4,12 @@ namespace common\modules\relation\controllers; @@ -4,8 +4,12 @@ namespace common\modules\relation\controllers;
4 4
5 use common\modules\rubrication\models\TaxOption; 5 use common\modules\rubrication\models\TaxOption;
6 use yii\base\Exception; 6 use yii\base\Exception;
  7 +use yii\db\ActiveQuery;
  8 +use yii\helpers\ArrayHelper;
  9 +use yii\web\Response;
7 use yii\data\ActiveDataProvider; 10 use yii\data\ActiveDataProvider;
8 use yii\db\ActiveRecord; 11 use yii\db\ActiveRecord;
  12 +use yii\filters\ContentNegotiator;
9 use yii\web\Controller; 13 use yii\web\Controller;
10 use Yii; 14 use Yii;
11 use common\modules\relation\relationHelper; 15 use common\modules\relation\relationHelper;
@@ -18,6 +22,20 @@ use yii\web\NotFoundHttpException; @@ -18,6 +22,20 @@ use yii\web\NotFoundHttpException;
18 */ 22 */
19 class ManageController extends Controller 23 class ManageController extends Controller
20 { 24 {
  25 + public function behaviors()
  26 + {
  27 + return [
  28 + /*'bootstrap' => [
  29 + 'class' => ContentNegotiator::className(),
  30 + 'only' => ['autocomplete'],
  31 + 'formats' => [ 'application/json' => Response::FORMAT_JSON],
  32 + 'languages' => [
  33 + 'ru',
  34 + ],
  35 + ],*/
  36 + ];
  37 + }
  38 +
21 /** 39 /**
22 * Renders the relations view 40 * Renders the relations view
23 * @return string 41 * @return string
@@ -66,14 +84,6 @@ class ManageController extends Controller @@ -66,14 +84,6 @@ class ManageController extends Controller
66 84
67 $model = new $relation['via']['model']; 85 $model = new $relation['via']['model'];
68 86
69 - $query1 = $relation['entity1']['model']::find();  
70 - if (!empty($relation['entity1']['where']))  
71 - $query1->where($relation['entity1']['where']);  
72 -  
73 - $query2 = $relation['entity2']['model']::find();  
74 - if (!empty($relation['entity2']['where']))  
75 - $query2->where($relation['entity2']['where']);  
76 -  
77 if ($model->load(Yii::$app->request->post())) { 87 if ($model->load(Yii::$app->request->post())) {
78 $model->save(); 88 $model->save();
79 return $this->redirect(['pars', 'relation' => $relation_key]); 89 return $this->redirect(['pars', 'relation' => $relation_key]);
@@ -81,8 +91,8 @@ class ManageController extends Controller @@ -81,8 +91,8 @@ class ManageController extends Controller
81 } else { 91 } else {
82 return $this->render('create', [ 92 return $this->render('create', [
83 'model' => $model, 93 'model' => $model,
84 - 'items1' => $query1->all(),  
85 - 'items2' => $query2->all(), 94 +// 'items1' => $query1->limit(100)->all(),
  95 +// 'items2' => $query2->asArray()->all(),
86 'relation_key' => $relation_key, 96 'relation_key' => $relation_key,
87 'relation' => $relation, 97 'relation' => $relation,
88 ]); 98 ]);
@@ -103,14 +113,6 @@ class ManageController extends Controller @@ -103,14 +113,6 @@ class ManageController extends Controller
103 113
104 $model = $this->findModel($relation_key, $id1, $id2); 114 $model = $this->findModel($relation_key, $id1, $id2);
105 115
106 - $query1 = $relation['entity1']['model']::find();  
107 - if (!empty($relation['entity1']['where']))  
108 - $query1->where($relation['entity1']['where']);  
109 -  
110 - $query2 = $relation['entity2']['model']::find();  
111 - if (!empty($relation['entity2']['where']))  
112 - $query2->where($relation['entity2']['where']);  
113 -  
114 if ($model->load(Yii::$app->request->post())) { 116 if ($model->load(Yii::$app->request->post())) {
115 $connection = Yii::$app->getDb(); 117 $connection = Yii::$app->getDb();
116 $transaction = $connection->beginTransaction(); 118 $transaction = $connection->beginTransaction();
@@ -137,8 +139,8 @@ class ManageController extends Controller @@ -137,8 +139,8 @@ class ManageController extends Controller
137 } else { 139 } else {
138 return $this->render('update', [ 140 return $this->render('update', [
139 'model' => $model, 141 'model' => $model,
140 - 'items1' => $query1->all(),  
141 - 'items2' => $query2->all(), 142 + 'value1' => $model->entity1->getAttribute($relation['entity1']['listField']),
  143 + 'value2' => $model->entity2->getAttribute($relation['entity2']['listField']),
142 'relation_key' => $relation_key, 144 'relation_key' => $relation_key,
143 'relation' => $relation, 145 'relation' => $relation,
144 ]); 146 ]);
@@ -178,6 +180,28 @@ class ManageController extends Controller @@ -178,6 +180,28 @@ class ManageController extends Controller
178 return $this->redirect(['pars', 'relation' => $relation_key]); 180 return $this->redirect(['pars', 'relation' => $relation_key]);
179 } 181 }
180 182
  183 + public function actionAutocomplete() {
  184 + $relation_key = Yii::$app->request->get('relation_key');
  185 + $entity = Yii::$app->request->get('entity');
  186 + $term = Yii::$app->request->get('term');
  187 + $relation_key = strtolower($relation_key);
  188 + $relation = relationHelper::getRelation($relation_key);
  189 +
  190 + /** @var ActiveQuery $query */
  191 + $query = $relation[$entity]['model']::find();
  192 +
  193 + if (!empty($relation[$entity]['searchJoin'])) {
  194 + $query->innerJoinWith($relation[$entity]['searchJoin']);
  195 + }
  196 + if (!empty($relation[$entity]['where'])) {
  197 + $query->where($relation[$entity]['where']);
  198 + }
  199 + $query->where(['ilike', $relation[$entity]['searchField'], $term]);
  200 +
  201 + print json_encode(ArrayHelper::map($query->limit(50)->all(), $relation[$entity]['key'], $relation[$entity]['listField']));
  202 + exit;
  203 + }
  204 +
181 /** 205 /**
182 * Finds the based model for relation on its primaries keys value. 206 * Finds the based model for relation on its primaries keys value.
183 * If the model is not found, a 404 HTTP exception will be thrown. 207 * If the model is not found, a 404 HTTP exception will be thrown.
common/modules/relation/views/manage/_form.php
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 use yii\helpers\Html; 3 use yii\helpers\Html;
4 use yii\widgets\ActiveForm; 4 use yii\widgets\ActiveForm;
5 use yii\helpers\ArrayHelper; 5 use yii\helpers\ArrayHelper;
  6 +use yii\jui\AutoComplete;
6 7
7 /* @var $this yii\web\View */ 8 /* @var $this yii\web\View */
8 /* @var $model common\modules\rubrication\models\TaxGroup */ 9 /* @var $model common\modules\rubrication\models\TaxGroup */
@@ -13,19 +14,40 @@ use yii\helpers\ArrayHelper; @@ -13,19 +14,40 @@ use yii\helpers\ArrayHelper;
13 14
14 <?php $form = ActiveForm::begin(); ?> 15 <?php $form = ActiveForm::begin(); ?>
15 16
16 - <?= $form->field($model, $relation['entity1']['linked_key'])->dropDownList(  
17 - ArrayHelper::map($items1, $relation['entity1']['key'], $relation['entity1']['listField']),  
18 - [  
19 - 'prompt' => Yii::t('relation', 'Select value'), 17 + <?= $form->field($model, $relation['entity1']['linked_key'])->widget(
  18 + AutoComplete::className(), [
  19 +// 'model' => \common\modules\relation\models\Relation::className(),
  20 + 'clientOptions' => [
  21 + 'source' => \yii\helpers\Url::to(['manage/autocomplete/', 'relation_key' => $relation_key, 'entity' => 'entity1']),
  22 + 'dataType' => 'json',
  23 + 'autoFill' => true,
  24 + 'minLength' => '2',
  25 + ],
  26 + 'options' => [
  27 + 'value' => empty($value1) ? '' : $value1,
  28 + 'class'=>'form-control',
  29 + 'placeholder' => $relation['entity1']['label']
20 ] 30 ]
21 - ) ?>  
22 -  
23 - <?= $form->field($model, $relation['entity2']['linked_key'])->dropDownList(  
24 - ArrayHelper::map($items2, $relation['entity2']['key'], $relation['entity2']['listField']),  
25 - [  
26 - 'prompt' => Yii::t('relation', 'Select value'), 31 + ]);
  32 + ?>
  33 +
  34 + <?= $form->field($model, $relation['entity2']['linked_key'])->widget(
  35 + AutoComplete::className(), [
  36 +// 'model' => \common\modules\relation\models\Relation::className(),
  37 + 'clientOptions' => [
  38 + 'source' => \yii\helpers\Url::to(['manage/autocomplete/', 'relation_key' => $relation_key, 'entity' => 'entity2']),
  39 + 'dataType' => 'json',
  40 + 'autoFill' => true,
  41 + 'minLength' => '2',
  42 + 'allowClear' => true,
  43 + ],
  44 + 'options' => [
  45 + 'value' => empty($value2) ? '' : $value2,
  46 + 'class'=>'form-control',
  47 + 'placeholder' => $relation['entity2']['label']
27 ] 48 ]
28 - ) ?> 49 + ]);
  50 + ?>
29 51
30 <div class="form-group"> 52 <div class="form-group">
31 <?= Html::submitButton($model->isNewRecord ? Yii::t('relation', 'Create') : Yii::t('relation', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 53 <?= Html::submitButton($model->isNewRecord ? Yii::t('relation', 'Create') : Yii::t('relation', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
common/modules/relation/views/manage/create.php
@@ -2,7 +2,6 @@ @@ -2,7 +2,6 @@
2 2
3 use yii\helpers\Html; 3 use yii\helpers\Html;
4 4
5 -  
6 /* @var $this yii\web\View */ 5 /* @var $this yii\web\View */
7 6
8 $this->title = Yii::t('relation', $relation['name']); 7 $this->title = Yii::t('relation', $relation['name']);
@@ -16,8 +15,6 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title; @@ -16,8 +15,6 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
16 15
17 <?= $this->render('_form', [ 16 <?= $this->render('_form', [
18 'model' => $model, 17 'model' => $model,
19 - 'items1' => $items1,  
20 - 'items2' => $items2,  
21 'relation_key' => $relation_key, 18 'relation_key' => $relation_key,
22 'relation' => $relation, 19 'relation' => $relation,
23 ]) ?> 20 ]) ?>
common/modules/relation/views/manage/update.php
@@ -16,8 +16,8 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title; @@ -16,8 +16,8 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
16 16
17 <?= $this->render('_form', [ 17 <?= $this->render('_form', [
18 'model' => $model, 18 'model' => $model,
19 - 'items1' => $items1,  
20 - 'items2' => $items2, 19 + 'value1' => $value1,
  20 + 'value2' => $value2,
21 'relation_key' => $relation_key, 21 'relation_key' => $relation_key,
22 'relation' => $relation, 22 'relation' => $relation,
23 ]) ?> 23 ]) ?>
common/modules/rubrication/models/TaxOption.php
@@ -166,7 +166,7 @@ class TaxOption extends \yii\db\ActiveRecord @@ -166,7 +166,7 @@ class TaxOption extends \yii\db\ActiveRecord
166 public function getValueRenderFlash() 166 public function getValueRenderFlash()
167 { 167 {
168 $valueClass = $this->getValueModelName(); 168 $valueClass = $this->getValueModelName();
169 - $value = $this->getValue()->one(); 169 + $value = $this->value;
170 if ($valueClass && method_exists($valueClass, 'getValueRenderFlash')) { 170 if ($valueClass && method_exists($valueClass, 'getValueRenderFlash')) {
171 return $valueClass::getValueRenderFlash($value); 171 return $valueClass::getValueRenderFlash($value);
172 } elseif(!empty($value)) { 172 } elseif(!empty($value)) {
@@ -181,7 +181,7 @@ class TaxOption extends \yii\db\ActiveRecord @@ -181,7 +181,7 @@ class TaxOption extends \yii\db\ActiveRecord
181 public function getValueRenderHTML() 181 public function getValueRenderHTML()
182 { 182 {
183 $valueClass = $this->getValueModelName(); 183 $valueClass = $this->getValueModelName();
184 - $value = $this->getValue()->one(); 184 + $value = $this->value;
185 if ($valueClass && method_exists($valueClass, 'getValueRenderHTML')) { 185 if ($valueClass && method_exists($valueClass, 'getValueRenderHTML')) {
186 return $valueClass::getValueRenderHTML($value); 186 return $valueClass::getValueRenderHTML($value);
187 } else { 187 } else {
@@ -219,7 +219,7 @@ class TaxOption extends \yii\db\ActiveRecord @@ -219,7 +219,7 @@ class TaxOption extends \yii\db\ActiveRecord
219 // } 219 // }
220 220
221 private function getValueModelName() { 221 private function getValueModelName() {
222 - $group = $this->getTaxGroup()->one(); 222 + $group = $this->taxGroup;
223 $valueClass = '\common\modules\rubrication\models\TaxValue'. ucfirst($group->module); 223 $valueClass = '\common\modules\rubrication\models\TaxValue'. ucfirst($group->module);
224 return class_exists($valueClass) ? $valueClass : FALSE; 224 return class_exists($valueClass) ? $valueClass : FALSE;
225 } 225 }
frontend/controllers/CatalogController.php
@@ -12,6 +12,7 @@ use common\modules\product\models\ProductSearch; @@ -12,6 +12,7 @@ use common\modules\product\models\ProductSearch;
12 use common\modules\product\models\ProductVariant; 12 use common\modules\product\models\ProductVariant;
13 use common\modules\rubrication\models\TaxGroup; 13 use common\modules\rubrication\models\TaxGroup;
14 use common\modules\rubrication\models\TaxOption; 14 use common\modules\rubrication\models\TaxOption;
  15 +use common\modules\rubrication\models\TaxValueString;
15 use yii\data\ActiveDataProvider; 16 use yii\data\ActiveDataProvider;
16 use yii\data\Pagination; 17 use yii\data\Pagination;
17 use yii\data\Sort; 18 use yii\data\Sort;
@@ -64,6 +65,7 @@ class CatalogController extends \yii\web\Controller @@ -64,6 +65,7 @@ class CatalogController extends \yii\web\Controller
64 $all_count = $query->count(); 65 $all_count = $query->count();
65 66
66 $brandsQuery = Brand::find() 67 $brandsQuery = Brand::find()
  68 + ->innerJoinWith('brandName')
67 ->innerJoinWith('products') 69 ->innerJoinWith('products')
68 ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. Product::tableName() .'.product_id') 70 ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. Product::tableName() .'.product_id')
69 ->where([ 71 ->where([
@@ -71,7 +73,7 @@ class CatalogController extends \yii\web\Controller @@ -71,7 +73,7 @@ class CatalogController extends \yii\web\Controller
71 ]) 73 ])
72 ->groupBy(Brand::tableName() .'.brand_id'); 74 ->groupBy(Brand::tableName() .'.brand_id');
73 $brands = $brandsQuery->all(); 75 $brands = $brandsQuery->all();
74 - $brands_count = $brandsQuery->count(); 76 + $brands_count = count($brands); // $brandsQuery->count();
75 77
76 $optionsQuery = TaxOption::find() 78 $optionsQuery = TaxOption::find()
77 ->select([ 79 ->select([
@@ -80,6 +82,7 @@ class CatalogController extends \yii\web\Controller @@ -80,6 +82,7 @@ class CatalogController extends \yii\web\Controller
80 ]) 82 ])
81 ->innerJoin(ProductOption::tableName(), ProductOption::tableName() .'.option_id='. TaxOption::tableName() .'.tax_option_id') 83 ->innerJoin(ProductOption::tableName(), ProductOption::tableName() .'.option_id='. TaxOption::tableName() .'.tax_option_id')
82 ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. ProductOption::tableName() .'.product_id') 84 ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. ProductOption::tableName() .'.product_id')
  85 + ->innerJoinWith('group')
83 ->where([ 86 ->where([
84 ProductCategory::tableName() .'.category_id' => $category->category_id 87 ProductCategory::tableName() .'.category_id' => $category->category_id
85 ]) 88 ])
@@ -133,12 +136,12 @@ class CatalogController extends \yii\web\Controller @@ -133,12 +136,12 @@ class CatalogController extends \yii\web\Controller
133 foreach($options_alias as &$option_alias) { 136 foreach($options_alias as &$option_alias) {
134 $option_alias = "'". $option_alias ."'"; 137 $option_alias = "'". $option_alias ."'";
135 } 138 }
136 - $group = TaxGroup::find()->where(['like', 'alias', $group_alias])->one(); 139 + /*$group = TaxGroup::find()->where(['like', 'alias', $group_alias])->one();
137 if (!$group) { 140 if (!$group) {
138 continue; 141 continue;
139 - }  
140 - $query->andWhere(Product::tableName() .'.product_id IN (SELECT product_id AS products FROM product_option INNER JOIN tax_option ON tax_option.tax_option_id = product_option.option_id WHERE tax_option.alias IN ('. implode(',', $options_alias) .'))'); 142 + }*/
141 } 143 }
  144 + $query->andWhere(Product::tableName() .'.product_id IN (SELECT product_id AS products FROM product_option INNER JOIN tax_option ON tax_option.tax_option_id = product_option.option_id WHERE tax_option.alias IN ('. implode(',', $options_alias) .'))');
142 } 145 }
143 146
144 if (($_brands = \Yii::$app->request->get('brand')) != false && is_array($_brands) && count($_brands) > 0) { 147 if (($_brands = \Yii::$app->request->get('brand')) != false && is_array($_brands) && count($_brands) > 0) {
@@ -152,12 +155,17 @@ class CatalogController extends \yii\web\Controller @@ -152,12 +155,17 @@ class CatalogController extends \yii\web\Controller
152 } 155 }
153 } 156 }
154 157
155 - $count = $query->count(); 158 + $query->with('variant');
  159 + $query->with('brand');
  160 + $query->with('categories');
  161 + $query->with('image');
156 162
  163 + $count = $query->count();
157 $pages = new Pagination(['totalCount' => $count, 'pageSize' => $per_page]); 164 $pages = new Pagination(['totalCount' => $count, 'pageSize' => $per_page]);
158 $query->offset($pages->offset) 165 $query->offset($pages->offset)
159 ->orderBy($sort->orders) 166 ->orderBy($sort->orders)
160 ->limit($pages->limit); 167 ->limit($pages->limit);
  168 +
161 $products = $query->all(); 169 $products = $query->all();
162 170
163 return $this->render( 171 return $this->render(
frontend/views/catalog/categories.php
@@ -14,7 +14,7 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title; @@ -14,7 +14,7 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
14 14
15 <div class="category_wrap_3_colum"><!-- 3 colons for items ================================== 1rst colum --> 15 <div class="category_wrap_3_colum"><!-- 3 colons for items ================================== 1rst colum -->
16 16
17 - <?php foreach($category->getAllChildrenTree(2) as $category) :?> 17 + <?php foreach($category->getAllChildrenTree(2, [], 'categoryName') as $category) :?>
18 <div class="wrap"> 18 <div class="wrap">
19 <div class="cat_li_cont"> 19 <div class="cat_li_cont">
20 <?php if (!empty($category['item']->image)) :?> 20 <?php if (!empty($category['item']->image)) :?>
frontend/views/catalog/products.php
@@ -2,11 +2,11 @@ @@ -2,11 +2,11 @@
2 /** @var $this \yii\web\View */ 2 /** @var $this \yii\web\View */
3 /** @var $dataProvider \yii\data\ActiveDataProvider */ 3 /** @var $dataProvider \yii\data\ActiveDataProvider */
4 4
5 -$this->title = $category->name; 5 +$this->title = $category->categoryName->value;
6 foreach($category->getParents()->all() as $parent) { 6 foreach($category->getParents()->all() as $parent) {
7 - $this->params['breadcrumbs'][] = ['label' => $parent->name, 'url' => ['catalog/category', 'alias' => $parent->alias]]; 7 + $this->params['breadcrumbs'][] = ['label' => $parent->categoryName->value, 'url' => ['catalog/category', 'alias' => $parent->alias]];
8 } 8 }
9 -$this->params['breadcrumbs'][] = $category->name; 9 +$this->params['breadcrumbs'][] = $category->categoryName->value;
10 ?> 10 ?>
11 <script type="text/javascript"> 11 <script type="text/javascript">
12 $(document).ready(function() { 12 $(document).ready(function() {
@@ -184,11 +184,11 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $category-&gt;name; @@ -184,11 +184,11 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $category-&gt;name;
184 184
185 <!-- catalog list with all item cards --> 185 <!-- catalog list with all item cards -->
186 <div class="cat_p_catalog_list"> 186 <div class="cat_p_catalog_list">
187 - <div class="title"><?= $category->name?> <span>(<?= $all_count?>)</span></div> 187 + <div class="title"><?= $category->categoryName->value?> <span>(<?= $all_count?>)</span></div>
188 188
189 <?php if (empty($products)) :?> 189 <?php if (empty($products)) :?>
190 <h2>По данному запросу товары не найдены.</h2><br> 190 <h2>По данному запросу товары не найдены.</h2><br>
191 - <p>Показать <a href="<?= \yii\helpers\Url::to(['catalog/category', 'alias' => $category->alias])?>">все товары из категории "<?= $category->name?>"</a></p> 191 + <p>Показать <a href="<?= \yii\helpers\Url::to(['catalog/category', 'alias' => $category->alias])?>">все товары из категории "<?= $category->categoryName->value?>"</a></p>
192 <?php else :?> 192 <?php else :?>
193 <!-- sort menu --> 193 <!-- sort menu -->
194 <div class="sort_menu"> 194 <div class="sort_menu">