【问题标题】:CodeIgniter Active Record method chaining with update not workingCodeIgniter Active Record 方法链接更新不起作用
【发布时间】:2015-02-25 01:00:51
【问题描述】:

我正在尝试使用 Active Record 方法来更新表并将任务标记为完成,如下所示:

$this->db->update('tasks', array('status' => 'complete'))
           ->where('id', $task_id);

但它给了我一个错误:

Call to a member function where() on a non-object

这里有什么我看不到的问题吗?还是方法链接不适用于更新?文档在方法链方面非常薄弱。

如果我把它分成两行,它确实有效......

$this->db->where('id', $task_id);
$this->db->update('tasks', array('status' => 'complete'));

但是方法链不应该在这里工作吗?

PHP 版本:5.5.4 CI版本:3

【问题讨论】:

    标签: php method-chaining phpactiverecord codeigniter-3


    【解决方案1】:

    找到答案了。

    update() 和 insert() 在方法被调用时执行,所以没有什么可以链接到,因为没有返回 db 对象。

    链接此插入语句的正确方法是反转它们:

    $this->db->where('id', $task_id)->update('tasks', array('status' => 'complete'));
    

    或者在更新函数上使用第三个选项参数:

    $this->db->update('tasks', array('status' => 'complete'), array('id', $task_id));
    

    希望这对其他人有所帮助。

    感谢 CI 社区 http://forum.codeigniter.com/thread-1281-post-5822.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多