Share.php 2.3 KB
<?php

namespace app\models;

use Yii;
use common\config\Params;

/**
 * This is the model class for table "{{%share}}".
 *
 * @property integer $id
 * @property integer $obj_id
 * @property integer $type
 * @property string $title
 * @property string $content
 * @property integer $create_time
 * @property integer $update_time
 */
class Share extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%share}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['obj_id', 'type', 'create_time', 'update_time'], 'integer'],
            [['content'], 'string'],
            [['pic'], 'string', 'max' => 200],
            [['title'], 'string', 'max' => 100],
            [['obj_id', 'type'], 'unique', 'targetAttribute' => ['obj_id', 'type'], 'message' => 'The combination of Obj ID and Type has already been taken.'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'obj_id' => 'Obj ID',
            'type' => 'Type',
            'pic' => '分享头图',
            'title' => '分享标题',
            'content' => '分享内容',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'createdAtAttribute' => 'create_time',
                'updatedAtAttribute' => 'update_time',
            ]
        ];
    }

    /**
     * 删除分享
     * @param $obj_id
     * @param $type
     * @return bool
     * @throws \Exception
     */
    private function del($obj_id,$type)
    {
        try{
            $rowCount = Yii::$app->db->createCommand()->delete(self::tableName(), ['obj_id'=>$obj_id,'type'=>$type])->execute();
            return ($rowCount>=0) ? true:false;
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }

    public function delLivingShare($room_id)
    {
        return $this->del($room_id,Params::LIVE_SHARE_TYPE);
    }

    public function delVideoShare($video_id)
    {
        return $this->del($video_id,Params::VIDEO_SHARE_TYPE);
    }
}