diff --git a/backend/controllers/CatalogController.php b/backend/controllers/CatalogController.php index 3910d8a..0471f25 100644 --- a/backend/controllers/CatalogController.php +++ b/backend/controllers/CatalogController.php @@ -85,7 +85,7 @@ class CatalogController extends Controller { $model = $this->findModel($id); $model_lang = CatalogLang::find() - ->where(['catalog' => $id, 'lang_id' => 1]) + ->where(['catalog_id' => $id, 'lang_id' => 1]) ->one(); if ($model->load(Yii::$app->request->post())) diff --git a/backend/models/SearchCatalog.php b/backend/models/SearchCatalog.php index baac514..6b3f278 100644 --- a/backend/models/SearchCatalog.php +++ b/backend/models/SearchCatalog.php @@ -11,8 +11,12 @@ use common\models\Catalog; * SearchCatalog represents the model behind the search form about `common\models\Catalog`. */ class SearchCatalog extends Catalog -{ - public $title; +{ + /** + * для поиска + * @var $title + */ + var $title; /** * @inheritdoc @@ -20,8 +24,7 @@ class SearchCatalog extends Catalog public function rules() { return [ - [['id', 'parent_id', 'type', 'subtype', 'status', 'sort'], 'integer'], - [['cover', 'options'], 'safe'], + [['catalog_id', 'parent_id', 'status', 'sort'], 'integer'], // + поиск по title [['title'], 'safe'] ]; @@ -54,7 +57,7 @@ class SearchCatalog extends Catalog // + поиск по title $dataProvider->setSort([ 'attributes' => [ - 'id', + 'catalog_id', 'title' => [ 'asc' => ['title' => SORT_ASC], 'desc' => ['title' => SORT_DESC], @@ -65,34 +68,31 @@ class SearchCatalog extends Catalog ]); $this->load($params); + + // + поиск по title + $query->joinWith(['relationCatalogLangPlus']); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); - - // + поиск по title - $query->joinWith(['catalog_i18n']); - + return $dataProvider; } $query->andFilterWhere([ - 'id' => $this->id, + 'catalog_id' => $this->catalog_id, 'parent_id' => $this->parent_id, - 'type' => $this->type, - 'subtype' => $this->subtype, 'status' => $this->status, 'sort' => $this->sort, ]); - $query->andFilterWhere(['like', 'cover', $this->cover]) - ->andFilterWhere(['like', 'options', $this->options]); + $query->andFilterWhere(['like', 'cover', $this->cover]); // + поиск по title if (! empty ($this->title)) { - $query->joinWith(['relationTable' => function ($q) + $query->joinWith(['relationCatalogLangPlus' => function ($q) { $q->where(['like', 'catalog_i18n.title', $this->title]); }]); diff --git a/backend/views/catalog/_form.php b/backend/views/catalog/_form.php index d37774e..e8b99db 100644 --- a/backend/views/catalog/_form.php +++ b/backend/views/catalog/_form.php @@ -2,6 +2,8 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; +use yii\helpers\ArrayHelper; +use common\models\Catalog; /* @var $this yii\web\View */ /* @var $model common\models\Catalog */ @@ -14,22 +16,37 @@ use yii\widgets\ActiveForm; = $form->field($model, 'title')->textInput() ?> - = $form->field($model, 'parent_id')->textInput() ?> - - = $form->field($model, 'type')->textInput() ?> - - = $form->field($model, 'subtype')->textInput() ?> + = $form->field($model, 'parent_id')->dropDownList( + ArrayHelper::map($model->find()->all(), 'parent_id', 'title') + ) + ?> = $form->field($model, 'cover')->textInput(['maxlength' => true]) ?> + + = $form->field($model, 'status')->dropDownList([ + '1' => Yii::t('action', 'show'), + '0' => Yii::t('action', 'hide'), + ]); + + echo '
'; + + $array = $model->finInfo([ + 'catalog_id' => 1, + 'return_one' => true, + 'to_array' => false, + ]); + + var_dump($array->relationCatalogLang2->title); + //var_dump($array->relationCatalogLang2->title); + + echo ''; - = $form->field($model, 'options')->textInput() ?> - - = $form->field($model, 'status')->textInput() ?> + ?> = $form->field($model, 'sort')->textInput() ?>
- = Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> - = Html::a(Yii::t('app', 'Delete'), ['delete', 'id' => $model->id], [ + = Html::a(Yii::t('app', 'Update'), ['update', 'catalog_id' => $model->catalog_id], ['class' => 'btn btn-primary']) ?> + = Html::a(Yii::t('app', 'Delete'), ['delete', 'catalog_id' => $model->catalog_id], [ 'class' => 'btn btn-danger', 'data' => [ 'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), @@ -28,12 +28,9 @@ $this->params['breadcrumbs'][] = $this->title; = DetailView::widget([ 'model' => $model, 'attributes' => [ - 'id', - 'parent_id', - 'type', - 'subtype', - 'cover', - 'options', + 'catalog_id', + 'parent_id', + 'cover', 'status', 'sort', ], diff --git a/common/models/Catalog.php b/common/models/Catalog.php index d26551b..72d4f80 100644 --- a/common/models/Catalog.php +++ b/common/models/Catalog.php @@ -3,21 +3,20 @@ namespace common\models; use Yii; +use yii\data\ActiveDataProvider; /** * This is the model class for table "catalog". * * @property integer $id - * @property integer $parent_id - * @property integer $type - * @property integer $subtype + * @property integer $parent_id * @property string $cover * @property string $options * @property integer $status * @property integer $sort */ class Catalog extends \yii\db\ActiveRecord -{ +{ /** * @inheritdoc */ @@ -32,40 +31,105 @@ class Catalog extends \yii\db\ActiveRecord public function rules() { return [ - [['parent_id', 'type', 'subtype', 'status', 'sort'], 'integer'], - [['options'], 'string'], + [['parent_id', 'status', 'sort'], 'integer'], [['cover'], 'string', 'max' => 32], ]; } - + /** * @inheritdoc */ public function attributeLabels() { return [ - 'id' => Yii::t('app', 'ID'), + 'catalog_id' => Yii::t('app', 'ID'), 'parent_id' => Yii::t('app', 'Parent ID'), - 'type' => Yii::t('app', 'Type'), - 'subtype' => Yii::t('app', 'Subtype'), - 'cover' => Yii::t('app', 'Cover'), - 'options' => Yii::t('app', 'Options'), + 'cover' => Yii::t('app', 'Cover'), 'status' => Yii::t('app', 'Status'), 'sort' => Yii::t('app', 'Sort'), ]; } + + /** + * Выполняет поис по параметрам + * @param array $param принимает [catalog_id, lang_id, return_one, return_field, show_all] + * @return array one | array all | string значение масива + */ + public function finInfo (array $params = []) + { + Tools::ifNotExist ($params, array ( + 'catalog_id' => false, + 'lang_id' => false, + 'return_one' => false, + 'return_field' => false, + 'show_all' => false, + 'to_array' => true, + )); + + $model = new Catalog(); + + $query = $model->find()->select('*'); + + $query->joinWith(['relationCatalogLang2']); + + $WHERE = array (); + + if ($params['catalog_id'] !== false) + { + $WHERE['catalog.catalog_id'] = $params['catalog_id']; + } + + if ($params['lang_id'] !== false) + { + $WHERE['catalog_i18n.lang_id'] = $params['lang_id']; + } + + if (! empty ($WHERE)) + { + $query->where($WHERE); + } + + if ($params['to_array'] !== false) + { + $query = $query->asArray(); + } + + if ($params['return_one'] !== false || $params['return_field'] !== false) + { + + $result = $params['return_field'] !== false ? $query->one($params['return_field']) : $query->one(); + } + else + { + $result = $query->all(); + } + + return $result; + } /** * @return \yii\db\ActiveQuery */ - public function getRelationTable() + public function getRelationCatalogLangPlus() + { + return $this->getRelationCatalogLang2()->where(['lang_id' => yii::$app->params['lang_id']]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getRelationCatalogLang2() { - return $this->hasOne(CatalogLang::className(), ['catalog' => 'id']); + return $this->hasOne(CatalogLang::className(), ['catalog_id' => 'catalog_id']); } - + public function getTitle() { - return $this->relationTable->title; + return $this->relationCatalogLangPlus->title; + } + + public function getParentTitle() + { + return $this->relationCatalogLangPlus->title; } - } diff --git a/common/models/CatalogLang.php b/common/models/CatalogLang.php index 8e31fa3..66dfc4d 100644 --- a/common/models/CatalogLang.php +++ b/common/models/CatalogLang.php @@ -7,7 +7,7 @@ use Yii; /** * This is the model class for table "catalog_i18n". * - * @property integer $catalog + * @property integer $catalog_id * @property integer $lang_id * @property string $title * @property string $alias @@ -32,8 +32,8 @@ class CatalogLang extends \yii\db\ActiveRecord public function rules() { return [ - [['catalog', 'lang_id', 'title', 'alias'], 'required'], - [['catalog', 'lang_id'], 'integer'], + [['catalog_id', 'lang_id', 'title', 'alias'], 'required'], + [['catalog_id', 'lang_id'], 'integer'], [['title'], 'string', 'max' => 1024], [['alias'], 'string', 'max' => 128], [['meta_title', 'meta_keywords', 'meta_description', 'full_alias'], 'string', 'max' => 255], @@ -47,7 +47,7 @@ class CatalogLang extends \yii\db\ActiveRecord public function attributeLabels() { return [ - 'catalog' => Yii::t('app', 'Catalog'), + 'catalog_id' => Yii::t('app', 'catalog_id'), 'lang_id' => Yii::t('app', 'Lang ID'), 'title' => Yii::t('app', 'Title'), 'alias' => Yii::t('app', 'Alias'), diff --git a/common/translation/ru/action.php b/common/translation/ru/action.php index 4420200..6eb8876 100644 --- a/common/translation/ru/action.php +++ b/common/translation/ru/action.php @@ -1,7 +1,7 @@ 'Добавить', + 'create' => 'Добавить', 'edit' => 'Изменить', 'update' => 'Обновить', 'del' => 'Удалить', -- libgit2 0.21.4