Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@4t4nner
Created August 14, 2019 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4t4nner/56172a4e03ae0102892b0928e31e34a0 to your computer and use it in GitHub Desktop.
Save 4t4nner/56172a4e03ae0102892b0928e31e34a0 to your computer and use it in GitHub Desktop.
unknown property error
<?php
namespace common\modules\news\models;
use common\modules\backend\components\behaviors\UploadImageBehavior;
use himiklab\sortablegrid\SortableGridBehavior;
use Yii;
use common\modules\user\models\User;
use common\modules\category\models\Category;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
/**
* This is the model class for table "{{%news}}".
*
* @property int $id
* @property int $category_id
* @property string $title
* @property string $slug
* @property string $short_text
* @property string $full_text
* @property string $image
* @property string $created_at
* @property string $updated_at
* @property int $sort
* @property int $status
* @property string $lang
*
* @property User $updatedBy
* @property User $updated_by
* @property Category $category
* @property User $created_by
*/
class News extends \yii\db\ActiveRecord
{
const STATUS_DISABLED = 0;
const STATUS_ACTIVE = 1;
// WARNING: если не вставить эти 2 переменные - возникает ошибка
public $created_by;
public $updated_by;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'news';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['category_id', 'sort', 'status'], 'integer'],
[['title', 'slug', 'full_text'], 'required'],
[['short_text', 'full_text'], 'string'],
[['created_at', 'updated_at', 'updated_by', 'created_by'], 'safe'],
[['title', 'slug'], 'string', 'max' => 255],
['image', 'image', 'extensions' => 'jpg, jpeg, gif, png', 'on' => ['insert', 'update']],
[['lang'], 'string', 'max' => 6],
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
];
}
/**
* @return array
*/
public function behaviors()
{
$module = Yii::$app->getModule('news');
return [
'sort' => [
'class' => SortableGridBehavior::className(),
'sortableAttribute' => 'sort',
],
[
'class' => TimestampBehavior::className(),
'value' => new Expression('NOW()'),
],
[
'class' => UploadImageBehavior::className(),
'attribute' => 'image',
'scenarios' => ['insert', 'update'],
'path' => $module->uploadPath,
],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('news/news', 'ID'),
'category_id' => Yii::t('news/news', 'Category'),
'title' => Yii::t('news/news', 'Title'),
'slug' => Yii::t('news/news', 'Slug'),
'short_text' => Yii::t('news/news', 'Short Text'),
'full_text' => Yii::t('news/news', 'Full Text'),
'image' => Yii::t('news/news', 'Image'),
'created_at' => Yii::t('news/news', 'Created At'),
'updated_at' => Yii::t('news/news', 'Updated At'),
'created_by' => Yii::t('news/news', 'Created By'),
'updated_by' => Yii::t('news/news', 'Updated By'),
'sort' => Yii::t('news/news', 'Sort'),
'status' => Yii::t('news/news', 'Status'),
'lang' => Yii::t('news/news', 'Language'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getUpdatedBy()
{
return $this->hasOne(User::className(), ['id' => 'updated_by']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
/**
* @return array
*/
public static function getStatusList()
{
return [
self::STATUS_DISABLED => Yii::t('news/news', 'Disabled'),
self::STATUS_ACTIVE => Yii::t('news/news', 'Active'),
];
}
/**
* @return int|mixed
*/
public function getStatusName()
{
$arr = $this::getStatusList();
return array_key_exists($this->status, $arr) ? $arr[$this->status] : $this->status;
}
/**
* @return array
*/
public static function getLanguageList()
{
// TODO: Получить список языков приложения.
return [
'ru-RU' => Yii::t('news/news', 'Russian'),
'en-US' => Yii::t('news/news', 'English'),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment