public function down()
 {
     $this->dropTable(Meta::tableName());
 }
Exemplo n.º 2
0
 /**
  * @param $uid
  * @param $type 操作类型, like 或 hate
  * @return bool|string string为错误提示, bool为操作成功还是失败
  */
 protected function toggleLikeOrHate($uid, $type)
 {
     //查找数据库是否有记录
     $model = Meta::find()->where(['or', ['type' => 'like'], ['type' => 'hate']])->andWhere(['uid' => $uid, 'target_id' => $this->id, 'target_type' => static::TYPE])->one();
     $contrary = $return = $active = false;
     if ($model) {
         $num = $model->delete();
         // 有记录(赞或踩)则取消记录
         if ($model->type == $type) {
             //相应记录删除后直接返回取消结果
             $return = $num >= 0;
         } else {
             $model = null;
             // 相对记录需清空查询结果已经生成相应的记录
             $contrary = true;
         }
     }
     if (!$model) {
         //创建记录
         $model = $type == Like::TYPE ? new Like() : new Hate();
         $model->setAttributes(array('uid' => $uid, 'target_id' => $this->id, 'target_type' => static::TYPE));
         if ($model->save()) {
             $return = $active = true;
         } else {
             $return = array_values($model->getFirstErrors())[0];
         }
     }
     if ($return == true) {
         // 更新记数
         $attributeName1 = $type . '_count';
         if ($contrary) {
             $attributeName2 = ($type == 'like' ? 'hate' : 'like') . '_count';
             $attributes = [$attributeName1 => $active ? 1 : ($this->{$attributeName1} > 0 ? -1 : 0), $attributeName2 => $active ? $this->{$attributeName2} > 0 ? -1 : 0 : 1];
         } else {
             $attributes = [$attributeName1 => $active ? 1 : ($this->{$attributeName1} > 0 ? -1 : 0)];
         }
         //更新版块统计
         $this->updateCounters($attributes);
     }
     return $return;
 }
 public function down()
 {
     $this->dropTable(User::tableName());
     $this->dropTable(Avatar::tableName());
     $this->dropTable(Meta::tableName());
 }