Skip to content

Instantly share code, notes, and snippets.

@calcio
Created March 17, 2018 11:17
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 calcio/ecbc94f98246ba069bd5ae52203e6143 to your computer and use it in GitHub Desktop.
Save calcio/ecbc94f98246ba069bd5ae52203e6143 to your computer and use it in GitHub Desktop.
Model Category Admin
<?php
namespace app\modules\admin\models;
use Yii;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "categories".
*
* @property integer $id
* @property string $name
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
*
* @property Products[] $products
*/
class Category extends yii\db\ActiveRecord
{
CONST STATUS_ACTIVE = 1;
CONST STATUS_INACTIVE = 0;
CONST STATUS_ACTIVE_STRING = 'Active';
CONST STATUS_INACTIVE_STRING = 'Inactive';
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%categories}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'status'], 'required'],
[['status'], 'integer'],
[['name'], 'string', 'max' => 80],
[['name'], 'unique'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'name' => Yii::t('app', 'Name'),
'status' => Yii::t('app', 'Status'),
'created_at' => Yii::t('app', 'Created At'),
'updated_at' => Yii::t('app', 'Updated At'),
];
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
],
];
}
public function getStatusItems()
{
return [
self::STATUS_INACTIVE => self::STATUS_INACTIVE_STRING,
self::STATUS_ACTIVE => self::STATUS_ACTIVE_STRING,
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProducts()
{
return $this->hasMany(Product::className(), ['category_id' => 'id']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment