Heim  >  Artikel  >  Backend-Entwicklung  >  Yii20 插入多条记录操作中,旧的属性值影响插入操作的原因

Yii20 插入多条记录操作中,旧的属性值影响插入操作的原因

WBOY
WBOYOriginal
2016-07-29 08:56:541300Durchsuche

我们在项目中有时会遇到插入和更新操作.

1: 对于更新来说:

      因为如果你使用$this->setOldAttributes(null);,那么代表清空了它原来的记录,它将默认为原来的旧属性不存在,那么他认为现在的这条记录是新的,所以它将进行插入操作,故我们不能将 它的旧属性清空.

2: 对于插入来说:

    我在插入操作中打印$this,它返回给我这样的结果:

private $_attributes => //这次现在的这条属性
  array(7) {
    'app_id' =>
    string(6) "100000"
    'hour' =>
    string(2) "21"
    'stat_day' =>
    string(10) "2015-07-13"
    'total' =>
    int(1)
    'id' =>
    string(16) "1000002015071321"
    'create_time' =>
    int(1449326053)
    'update_time' =>
    int(1449326053)
  }
  private $_oldAttributes =>   //此时,它将旧的属性给与了此时的值
  array(7) {
    'app_id' =>
    string(6) "100000"
    'hour' =>
    string(2) "21"
    'stat_day' =>
    string(10) "2015-07-13"
    'total' =>
    int(1)
    'id' =>
    string(16) "1000002015071321"
    'create_time' =>
    int(1449326053)
    'update_time' =>
    int(1449326053)
  }
  private $_related =>
  array(0) {
  }
  private $_errors =>
  array(0) {
  }
  private $_validators =>
  class ArrayObject#48 (1) {
    private $storage =>
    array(0) {
    }
  }
  private $_scenario =>
  string(7) "default"
  private $_events =>
  array(0) {
  }
  private $_behaviors =>
  array(0) {
  }
}

当我们"插入"第一条记录的时候, 程序会将此时的这条记录, 记录到旧属性里面, 也就是上面的$_oldAttributes属性里面, 那么我们下面"插入"第2条记录的时候, 程序查看到对象中$_oldAttributes属性中有值(不管他是什么值), 都默认为此记录已经存在,所以它将进行"更新"操作, 但是我们现在目的是让程序进行"插入"操作啊, 怎么能让它"更新"那,所以此时我们在每次"插入"的时候, 使用$this->setOldAttributes(null)方式, 将$_oldAttributes清空一下, 那么任何时候, 后面再"插入"新纪录的时候,程序查看$_oldAttributes属性为null, 所以自然而然的就按照我们的操作去"插入"新的记录了啊!!

     我们使用了$this->setOldAttributes(null); 来清空旧的属性, 让下面需要新的记录插入进去.

现在我项目中的例子:

public function actionOfflineUserImport()
    {
        $mTempUser = new TempUser();
        //取出学霸表中的所有的用户信息
        $tempUsers = $mTempUser->find()->select('mobile, truename, weixin, corp, position')->asArray()->all();

        if (!$tempUsers) {
            echo  "无用户需要同步\n";
            exit(2);
        }

        // 遍历所有的用户信息,并将每一项插入到另一个数据库中表中
        foreach ($tempUsers as $key => $values) {
            /<span style="background-color: rgb(0, 153, 0);">/重点在这里</span>: 我们需要每次都重新实例化对象,不然他就会出现上面的情况旧的属性存在,导致他们不认为是添加数据
              这里我采用的和上面的不一样,如果你有很多条数据,你传递到model中,需要遍历添加数据,这种情况你可以用上面的清空属性的方法来解决
            <span style="color:#CC0000;">$mAccount = new Account();
            $mHmcUser = new HmcUser();</span>  <span style="color:#CC66CC;">之前我没把这两个对象放到遍历里面,而是放到了方法头部,导致了我添加数据不成功 ??这里一定要注意啊!!!</span>

            if (!isset($values['mobile']) || !$values['mobile']) {
                echo "手机号码不存在\n";
                exit(3);
            }

            $mobile = $values['mobile'];
            $isMobile = $mAccount->isExistMobile($mobile);

            if (!$isMobile) {

                try {
                    $key = 'user:id:pool';
                    $cache = Yii::$app->cache->instance('base');

                    if (!($cache->lpop($key))) {
                        echo  "获取用户ID失败\n";
                        exit(4);
                    } else {
                        $values['user_id'] = $cache->lpop($key);
                    }
                } catch (\Exception $e) {
                    echo  "连接redis服务器失败,请稍后重试\n";
                    exit(5);
                }

                $data['user_id'] = $values['user_id'];
                $data['mobile'] = $values['mobile'];
                $accountData = $mAccount->addCrm($data, $status = Account::STATUS_UNINIT);
                $usersData = $mHmcUser->add($values);

                if (!$accountData) {
                    echo  $mobile . "同步account表 => 失败\n";
                } else {
                    echo  $mobile . "同步account表 => 成功\n";
                }

                if (!$usersData) {
                    echo  $mobile . "同步到 user表 => 失败\n";
                } else {
                    echo  $mobile . "同步到 user表 => 成功\n";
                }
            }
        }

        exit(0);

    }

补充:

        今天刚知道一个在命令行下可以输入: echo $?  显示的是上一条命令执行的状态(码)

           0表示上一条命令执行成功

以上就介绍了Yii20 插入多条记录操作中,旧的属性值影响插入操作的原因,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn