2018-11-12 00:15:11 1561次浏览 4条回答 0 悬赏 30 金钱
$model_customer = new Customer();

$sender_id = $model_customer->addCustomer($this->sender, $this->s_tel, $this->s_city, $this->s_place, $this->s_city_code);

$receiver_id = $model_customer->addCustomer($this->receiver, $this->r_tel, $this->r_city, $this->r_place, $this->r_city_code);

上面的 addCustomer() 是一段赋值和save操作,这么写为什么只有第二次addCustomer生效?

如果把 $model_customer 换成 (new Customer()) 两次就都能执行成功.这是为什么

最佳答案

  • liujingxing 发布于 2018-11-12 09:44 举报
    • 第一种写法:你new 出来的对象只有一个,等于在一个对象上操作了两次 save() 第一次是 insert 第二次是 update
    • 第二种写法:你是new 出的两个对象,都是在执行的insert 方法
      save() 方法,会自动判断执行insert 还是 update 方法,当对象是第一次实例化出来,执行的是insert, 查询出来的对象执行的是update,具体可以看文档说明
    觉得很赞
  • 回答于 2018-11-12 10:00 举报
    $model_customer = new Customer();
    
    $sender_id = $model_customer->addCustomer($this->sender, $this->s_tel, $this->s_city, $this->s_place, $this->s_city_code);
    
    $model_customer = new Customer(); //这样第一次的就不会被第二次的覆盖造成看上去只有第二次生效了
    $receiver_id = $model_customer->addCustomer($this->receiver, $this->r_tel, $this->r_city, $this->r_place, $this->r_city_code);
    
    觉得很赞
  • 回答于 2018-11-12 13:57 举报

    请你把addCustomer方法的源码贴出来好吗?这样大家看得都简单些,不用那么多语言解释。

  • 回答于 2018-11-13 09:17 举报

    看一楼的回答

您需要登录后才可以回答。登录 | 立即注册
jorge
见习主管

jorge

注册时间:2016-10-31
最后登录:2024-03-01
在线时长:15小时25分
  • 粉丝0
  • 金钱90
  • 威望0
  • 积分240

热门问题