Commit 48269590f2997f6618647a088471eb31d2268106

Authored by andryeyev
1 parent 754fc7d9

+ Страница getDataByKey метод

backend/controllers/PageController.php
... ... @@ -8,6 +8,7 @@ use backend\models\SearchPage;
8 8 use yii\web\Controller;
9 9 use yii\web\NotFoundHttpException;
10 10 use yii\filters\VerbFilter;
  11 +use common\models\PageLang;
11 12  
12 13 /**
13 14 * PageController implements the CRUD actions for Page model.
... ... @@ -62,10 +63,24 @@ class PageController extends Controller
62 63 public function actionCreate()
63 64 {
64 65 $model = new Page();
65   -
66   - if ($model->load(Yii::$app->request->post()) && $model->save()) {
  66 +
  67 + if ($model->load(Yii::$app->request->post()) && $model->save())
  68 + {
  69 + // сохраняем в таблицу page_lang
  70 + $model2 = new PageLang();
  71 +
  72 + // передаем переменные
  73 + $model2->attributes = $_POST['Page'];
  74 + $model2->page_id = $model->page_id;
  75 + $model2->lang_id = Yii::$app->lang_id;
  76 +
  77 + // сохраняем
  78 + $model2->save();
  79 +
67 80 return $this->redirect(['view', 'id' => $model->page_id]);
68   - } else {
  81 + }
  82 + else
  83 + {
69 84 return $this->render('create', [
70 85 'model' => $model,
71 86 ]);
... ... @@ -79,13 +94,22 @@ class PageController extends Controller
79 94 * @return mixed
80 95 */
81 96 public function actionUpdate($id)
82   - {
  97 + {
83 98 $model = $this->findModel($id);
84 99  
85   - if ($model->load(Yii::$app->request->post()) && $model->save()) {
  100 + if ($model->load(Yii::$app->request->post()) && $model->save())
  101 + {
  102 + // сохраняем в таблицу page_lang
  103 + $model2 = new PageLang();
  104 +
  105 + // сохраняем
  106 + $model2->save();
  107 +
86 108 return $this->redirect(['view', 'id' => $model->page_id]);
87   - } else {
88   - return $this->render('update', [
  109 + }
  110 + else
  111 + {
  112 + return $this->render('create', [
89 113 'model' => $model,
90 114 ]);
91 115 }
... ... @@ -113,7 +137,7 @@ class PageController extends Controller
113 137 */
114 138 protected function findModel($id)
115 139 {
116   - if (($model = Page::findOne($id)) !== null) {
  140 + if (($model = Page::findOne(['page_id' => $id])) !== null) {
117 141 return $model;
118 142 } else {
119 143 throw new NotFoundHttpException('The requested page does not exist.');
... ...
common/messages/uk/field.php
1 1 <?php
2 2  
3 3 return [
4   - 'page|' => 'Сторінка',
  4 + 'page' => 'Сторінка',
5 5 'date_add' => 'Дата додання',
6 6 'template' => 'Шаблон',
7 7 'image' => 'Картинка',
... ...
common/models/Page.php
... ... @@ -16,19 +16,8 @@ use common\models\PageLang;
16 16 */
17 17 class Page extends \yii\db\ActiveRecord
18 18 {
19   - private static $getAttributeLabelCache;
20   -
21   - public function getAttributeLabel($attribute)
22   - {
23   - $class = get_class($this);
24   -
25   - if (!isset(self::$getAttributeLabelCache[$class][$attribute])) {
26   - self::$getAttributeLabelCache[$class][$attribute] = parent::getAttributeLabel($attribute);
27   - }
28   -
29   - return self::$getAttributeLabelCache[$class][$attribute];
30   - }
31   -
  19 + var $data;
  20 +
32 21 //public $title;
33 22 /**
34 23 * @inheritdoc
... ... @@ -38,7 +27,24 @@ class Page extends \yii\db\ActiveRecord
38 27 return 'page';
39 28 }
40 29  
41   - // ==== EXTRA ====
  30 + // ==== DATA PAGE LANG ====
  31 +
  32 + public function getData()
  33 + {
  34 + $this->data = PageLang::find()->where(['page_id' => $this->page_id, 'lang_id' => 1])->one();
  35 + }
  36 +
  37 + public function getDataByKey($key)
  38 + {
  39 + if (! $this->data)
  40 + {
  41 + $this->getData();
  42 + }
  43 +
  44 + return isset ($this->data[$key]) ? $this->data[$key] : 'NO FIELD';
  45 + }
  46 +
  47 + // ==== DATA PAGE LANG FIELD ====
42 48  
43 49 public function getExtraField($key)
44 50 {
... ... @@ -47,27 +53,27 @@ class Page extends \yii\db\ActiveRecord
47 53  
48 54 public function getTitle()
49 55 {
50   - return $this->getExtraField('title');
  56 + return $this->getDataByKey('title');
51 57 }
52 58  
53 59 public function getMeta_title()
54 60 {
55   - return $this->getExtraField('meta_title');
  61 + return $this->getDataByKey('meta_title');
56 62 }
57 63  
58 64 public function getMeta_description()
59 65 {
60   - return $this->getExtraField('meta_description');
  66 + return $this->getDataByKey('meta_description');
61 67 }
62 68  
63 69 public function getText()
64 70 {
65   - return $this->getExtraField('text');
  71 + return $this->getDataByKey('text');
66 72 }
67 73  
68 74 public function getPage_alias()
69 75 {
70   - return $this->getExtraField('page_alias');
  76 + return $this->getDataByKey('page_alias');
71 77 }
72 78  
73 79 /**
... ... @@ -101,5 +107,11 @@ class Page extends \yii\db\ActiveRecord
101 107 'page_alias' => Yii::t('field', 'page_alias'),
102 108 'lang_id' => Yii::t('field', 'lang_id'),
103 109 ];
104   - }
  110 + }
  111 +/*
  112 + public function getPage()
  113 + {
  114 + return $this->hasMany(PageLang::className(), ['page_id' => 'page_id']);
  115 + }
  116 +*/
105 117 }
... ...
common/models/PageLang.php
... ... @@ -31,7 +31,7 @@ class PageLang extends \yii\db\ActiveRecord
31 31 public function rules()
32 32 {
33 33 return [
34   - [['page_id', 'title', 'meta_title', 'meta_description', 'text', 'page_alias', 'lang_id'], 'required'],
  34 + [['page_id', 'title', 'meta_title', 'meta_description', 'text', 'page_alias', 'lang_id'], 'safe'],
35 35 [['page_id', 'lang_id'], 'integer'],
36 36 [['text'], 'string'],
37 37 [['title', 'page_alias'], 'string', 'max' => 256],
... ...