101 lines
2.5 KiB
PHP
101 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/5/17
|
|
* Time: 18:22
|
|
*/
|
|
|
|
namespace common\models;
|
|
|
|
|
|
use common\services\ItemServices;
|
|
use yii\behaviors\TimestampBehavior;
|
|
use yii\db\ActiveRecord;
|
|
|
|
/**
|
|
* 物品
|
|
* @property int $id
|
|
* @property int $user_id 所属用户ID
|
|
* @property int $item_id 物品ID
|
|
* @property string $name 物品名称
|
|
* @property int $type 物品类型
|
|
* @property int $num 数量
|
|
* @property integer $created_at
|
|
* @property integer $updated_at
|
|
*/
|
|
class Item extends ActiveRecord
|
|
{
|
|
public function behaviors()
|
|
{
|
|
return [
|
|
TimestampBehavior::className(),
|
|
];
|
|
}
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
['id', 'default', 'value' => 0],
|
|
|
|
['user_id', 'required'],
|
|
['user_id', 'exist', 'targetClass' => '\common\models\User', 'targetAttribute' => 'id'],
|
|
['user_id', 'number', 'integerOnly' => true],
|
|
|
|
['item_id', 'default', 'value' => 0],
|
|
['item_id', 'number', 'integerOnly' => true],
|
|
|
|
['name', 'default', 'value' => ''],
|
|
|
|
['type', 'default', 'value' => 0],
|
|
['type', 'number', 'integerOnly' => true],
|
|
|
|
['num', 'default', 'value' => 1],
|
|
['num', 'number', 'integerOnly' => true],
|
|
|
|
['created_at', 'default', 'value' => 0],
|
|
['updated_at', 'default', 'value' => 0],
|
|
];
|
|
}
|
|
|
|
public static function findByID($id)
|
|
{
|
|
return self::findOne(['id' => $id]);
|
|
}
|
|
|
|
public static function findByUserIDAndItemID($userID, $itemID)
|
|
{
|
|
return self::findOne(['user_id' => $userID, 'item_id' => $itemID]);
|
|
}
|
|
|
|
public function __get($name)
|
|
{
|
|
if($name=='type_str') return ItemServices::ITEM_TYPE_STR[$this->type];
|
|
if($name=='created_at_str') return date('Y-m-d H:i:s',$this->created_at);
|
|
if($name=='updated_at_str') return date('Y-m-d H:i:s',$this->updated_at);
|
|
return parent::__get($name);
|
|
}
|
|
|
|
public function attributeLabels()
|
|
{
|
|
$data=parent::attributeLabels();
|
|
$data['item_id']='物品ID';
|
|
$data['name']='物品名称';
|
|
$data['type_str']='物品类型';
|
|
$data['num']='数量';
|
|
return $data;
|
|
}
|
|
|
|
public function genClientData()
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'user_id' => $this->user_id,
|
|
'item_id' => $this->item_id,
|
|
'name' => $this->name,
|
|
'type' => $this->type,
|
|
'num' => $this->num,
|
|
];
|
|
}
|
|
|
|
} |