108 lines
3.0 KiB
PHP
108 lines
3.0 KiB
PHP
<?php
|
||
|
||
namespace app\models;
|
||
|
||
use Yii;
|
||
|
||
/**
|
||
* This is the model class for table "{{%cart}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property int $user_id 用户ID
|
||
* @property int $goods_id 商品ID
|
||
* @property int $attr_id 规格ID
|
||
* @property int $num 数量
|
||
* @property int $is_delete 是否删除
|
||
* @property string|null $plugin_sign 插件标识
|
||
* @property int $created_at 添加时间
|
||
* @property int $updated_at 更新时间
|
||
* @property int $deleted_at 删除时间
|
||
* @property string|null $attr_info 规格信息,JSON
|
||
*/
|
||
class Cart extends \yii\db\ActiveRecord
|
||
{
|
||
const CART_STATUS_CACHE = 'cart_status_cache';
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%cart}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['attr_info', 'plugin_sign'], 'trim'],
|
||
[['cx_mch_id', 'user_id', 'goods_id', 'attr_id', 'num', 'is_delete', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
|
||
[['user_id', 'goods_id'], 'required'],
|
||
[['attr_info'], 'string'],
|
||
[['plugin_sign'], 'string', 'max' => 50],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'user_id' => '用户ID',
|
||
'goods_id' => '商品ID',
|
||
'attr_id' => '规格ID',
|
||
'num' => '数量',
|
||
'is_delete' => '是否删除',
|
||
'plugin_sign' => '插件标识',
|
||
'created_at' => '添加时间',
|
||
'updated_at' => '更新时间',
|
||
'deleted_at' => '删除时间',
|
||
'attr_info' => '规格信息,JSON',
|
||
];
|
||
}
|
||
|
||
|
||
public function beforeSave($insert) {
|
||
if(parent::beforeSave($insert)){
|
||
if($this->isNewRecord){
|
||
$this->created_at = time();
|
||
}
|
||
$this->updated_at = time();
|
||
if($this->is_delete == 1)
|
||
$this->deleted_at = time();
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
public static function cacheStatusGet()
|
||
{
|
||
$cart_status_cache = self::CART_STATUS_CACHE . \Yii::$app->user->identity->id;
|
||
return \Yii::$app->cache->get($cart_status_cache);
|
||
}
|
||
|
||
public static function cacheStatusSet(bool $info)
|
||
{
|
||
$cart_status_cache = self::CART_STATUS_CACHE . \Yii::$app->user->identity->id;
|
||
\Yii::$app->cache->set($cart_status_cache, $info, 0);
|
||
}
|
||
|
||
public function getAttrs()
|
||
{
|
||
return $this->hasOne(GoodsAttr::className(), ['id' => 'attr_id'])->where(['is_delete' => 0]);
|
||
}
|
||
|
||
public function getGoods()
|
||
{
|
||
return $this->hasOne(Goods::className(), ['id' => 'goods_id'])->where(['is_delete' => 0]);
|
||
}
|
||
}
|