При публикации записи в Блог добавить поле Автор

parent 504b17b3
...@@ -7,6 +7,7 @@ use Yii; ...@@ -7,6 +7,7 @@ use Yii;
use common\modules\languages\models\Languages; use common\modules\languages\models\Languages;
use common\modules\blog\models\PostLang; use common\modules\blog\models\PostLang;
use common\modules\blog\models\PostTag; use common\modules\blog\models\PostTag;
use common\modules\users\models\User;
use common\models\MetaTags; use common\models\MetaTags;
/** /**
...@@ -52,7 +53,7 @@ class Post extends \common\components\ActiveRecordModel ...@@ -52,7 +53,7 @@ class Post extends \common\components\ActiveRecordModel
{ {
return [ return [
[['url', 'active'], 'required'], [['url', 'active'], 'required'],
[['active', 'created_at', 'updated_at'], 'integer'], [['active', 'created_at', 'updated_at', 'author_id'], 'integer'],
[['url'], 'string', 'max' => 255], [['url'], 'string', 'max' => 255],
[['url'], 'unique'], [['url'], 'unique'],
[['preview', 'unlinkFile', 'tags'], 'safe'], [['preview', 'unlinkFile', 'tags'], 'safe'],
...@@ -100,6 +101,7 @@ class Post extends \common\components\ActiveRecordModel ...@@ -100,6 +101,7 @@ class Post extends \common\components\ActiveRecordModel
{ {
return [ return [
'id' => 'ID', 'id' => 'ID',
'author_id' => 'Автор',
'url' => 'Ссылка', 'url' => 'Ссылка',
'active' => 'Видимость', 'active' => 'Видимость',
'file' => 'Изображение', 'file' => 'Изображение',
...@@ -173,10 +175,31 @@ class Post extends \common\components\ActiveRecordModel ...@@ -173,10 +175,31 @@ class Post extends \common\components\ActiveRecordModel
return $this->hasMany(PostTagAssign::className(), ['post_id' => 'id']); return $this->hasMany(PostTagAssign::className(), ['post_id' => 'id']);
} }
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthor()
{
return $this->hasOne(User::className(), ['id' => 'author_id']);
}
public function getThumbnailUrl() public function getThumbnailUrl()
{ {
$path = pathinfo($this->preview); $path = pathinfo($this->preview);
return $path['dirname'] . '/' . $path['filename'] . '.thumb.' . $path['extension']; return $path['dirname'] . '/' . $path['filename'] . '.thumb.' . $path['extension'];
} }
public function beforeSave($insert)
{
if (parent::beforeSave($insert))
{
$this->author_id = Yii::$app->user->identity->id;
return true;
}
else
{
return false;
}
}
} }
...@@ -61,6 +61,18 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -61,6 +61,18 @@ $this->params['breadcrumbs'][] = $this->title;
return null; return null;
} }
], ],
[
'attribute' => 'author_id',
'value' => function($model)
{
if($model->author)
{
return $model->author->surname . ' ' . $model->author->name;
}
return null;
}
],
[ [
'class' => 'common\components\ColorActionColumn', 'class' => 'common\components\ColorActionColumn',
......
<?php
use yii\db\Schema;
use yii\db\Migration;
class m160219_112010_add_post_column extends Migration
{
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
$this->addColumn('posts', 'author_id', Schema::TYPE_INTEGER . '(11) NOT NULL AFTER `active`');
}
public function safeDown()
{
$this->dropColumn('posts', 'author_id');
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment