From 48269590f2997f6618647a088471eb31d2268106 Mon Sep 17 00:00:00 2001 From: andryeyev Date: Fri, 6 Nov 2015 14:30:17 +0200 Subject: [PATCH] + Страница getDataByKey метод --- backend/controllers/PageController.php | 40 ++++++++++++++++++++++++++++++++-------- common/messages/uk/field.php | 2 +- common/models/Page.php | 52 ++++++++++++++++++++++++++++++++-------------------- common/models/PageLang.php | 2 +- 4 files changed, 66 insertions(+), 30 deletions(-) diff --git a/backend/controllers/PageController.php b/backend/controllers/PageController.php index 8891e8a..5e174b5 100644 --- a/backend/controllers/PageController.php +++ b/backend/controllers/PageController.php @@ -8,6 +8,7 @@ use backend\models\SearchPage; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; +use common\models\PageLang; /** * PageController implements the CRUD actions for Page model. @@ -62,10 +63,24 @@ class PageController extends Controller public function actionCreate() { $model = new Page(); - - if ($model->load(Yii::$app->request->post()) && $model->save()) { + + if ($model->load(Yii::$app->request->post()) && $model->save()) + { + // сохраняем в таблицу page_lang + $model2 = new PageLang(); + + // передаем переменные + $model2->attributes = $_POST['Page']; + $model2->page_id = $model->page_id; + $model2->lang_id = Yii::$app->lang_id; + + // сохраняем + $model2->save(); + return $this->redirect(['view', 'id' => $model->page_id]); - } else { + } + else + { return $this->render('create', [ 'model' => $model, ]); @@ -79,13 +94,22 @@ class PageController extends Controller * @return mixed */ public function actionUpdate($id) - { + { $model = $this->findModel($id); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ($model->load(Yii::$app->request->post()) && $model->save()) + { + // сохраняем в таблицу page_lang + $model2 = new PageLang(); + + // сохраняем + $model2->save(); + return $this->redirect(['view', 'id' => $model->page_id]); - } else { - return $this->render('update', [ + } + else + { + return $this->render('create', [ 'model' => $model, ]); } @@ -113,7 +137,7 @@ class PageController extends Controller */ protected function findModel($id) { - if (($model = Page::findOne($id)) !== null) { + if (($model = Page::findOne(['page_id' => $id])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); diff --git a/common/messages/uk/field.php b/common/messages/uk/field.php index 69eee6f..d78515a 100644 --- a/common/messages/uk/field.php +++ b/common/messages/uk/field.php @@ -1,7 +1,7 @@ 'Сторінка', + 'page' => 'Сторінка', 'date_add' => 'Дата додання', 'template' => 'Шаблон', 'image' => 'Картинка', diff --git a/common/models/Page.php b/common/models/Page.php index 10f519f..9e38186 100644 --- a/common/models/Page.php +++ b/common/models/Page.php @@ -16,19 +16,8 @@ use common\models\PageLang; */ class Page extends \yii\db\ActiveRecord { - private static $getAttributeLabelCache; - - public function getAttributeLabel($attribute) - { - $class = get_class($this); - - if (!isset(self::$getAttributeLabelCache[$class][$attribute])) { - self::$getAttributeLabelCache[$class][$attribute] = parent::getAttributeLabel($attribute); - } - - return self::$getAttributeLabelCache[$class][$attribute]; - } - + var $data; + //public $title; /** * @inheritdoc @@ -38,7 +27,24 @@ class Page extends \yii\db\ActiveRecord return 'page'; } - // ==== EXTRA ==== + // ==== DATA PAGE LANG ==== + + public function getData() + { + $this->data = PageLang::find()->where(['page_id' => $this->page_id, 'lang_id' => 1])->one(); + } + + public function getDataByKey($key) + { + if (! $this->data) + { + $this->getData(); + } + + return isset ($this->data[$key]) ? $this->data[$key] : 'NO FIELD'; + } + + // ==== DATA PAGE LANG FIELD ==== public function getExtraField($key) { @@ -47,27 +53,27 @@ class Page extends \yii\db\ActiveRecord public function getTitle() { - return $this->getExtraField('title'); + return $this->getDataByKey('title'); } public function getMeta_title() { - return $this->getExtraField('meta_title'); + return $this->getDataByKey('meta_title'); } public function getMeta_description() { - return $this->getExtraField('meta_description'); + return $this->getDataByKey('meta_description'); } public function getText() { - return $this->getExtraField('text'); + return $this->getDataByKey('text'); } public function getPage_alias() { - return $this->getExtraField('page_alias'); + return $this->getDataByKey('page_alias'); } /** @@ -101,5 +107,11 @@ class Page extends \yii\db\ActiveRecord 'page_alias' => Yii::t('field', 'page_alias'), 'lang_id' => Yii::t('field', 'lang_id'), ]; - } + } +/* + public function getPage() + { + return $this->hasMany(PageLang::className(), ['page_id' => 'page_id']); + } +*/ } diff --git a/common/models/PageLang.php b/common/models/PageLang.php index b73795a..e5a23d3 100644 --- a/common/models/PageLang.php +++ b/common/models/PageLang.php @@ -31,7 +31,7 @@ class PageLang extends \yii\db\ActiveRecord public function rules() { return [ - [['page_id', 'title', 'meta_title', 'meta_description', 'text', 'page_alias', 'lang_id'], 'required'], + [['page_id', 'title', 'meta_title', 'meta_description', 'text', 'page_alias', 'lang_id'], 'safe'], [['page_id', 'lang_id'], 'integer'], [['text'], 'string'], [['title', 'page_alias'], 'string', 'max' => 256], -- libgit2 0.21.4