whiteatom
whiteatom

Reputation: 1455

CodeIgniter Active Record method chaining with update not working

I'm trying to use Active Record methods to update a table and flag a task as complete like this:

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

But it's giving me an error:

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

Is there something wrong here that I can't see here? or does method chaining not work with update? The docs are pretty thin on method chaining..

It does work if I break it into two lines...

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

but shouldn't method chaining work here?

PHP version: 5.5.4 CI version: 3

Upvotes: 1

Views: 1250

Answers (1)

whiteatom
whiteatom

Reputation: 1455

Found the answer.

update() and insert() execute when the method is called, so there is nothing to chain to because the db object is not returned.

The correct way to chain this insert statement is to reverse them:

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

Or to use the 3rd option parameter on the update function:

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

Hope this helps someone else.

Credit to the CI community at http://forum.codeigniter.com/thread-1281-post-5822.html

Upvotes: 1

Related Questions