Welcome Guest, Not a member yet? Register   Sign In
How to check for success on Active Record's Class Operations
#1

[eluser]Daniel Peraza[/eluser]
Hi there!.

I'm beginning with CI, although I already have some PHP experience. I have read the user guide but there is one thing unclear to me: how should I check for unsuccessful model operations?, let's say that I try to delete a not existent record or that my query has syntax errors, then how should I check the Active Record Class method's return values within my controller?

I'm a little confuse since some methods of the ARC return other ARC objects, which in turn, is what permits method chaining to my understand.
#2

[eluser]Pascal Kriete[/eluser]
AR is chainable when you're building your query. What you want to do is to check what happened once it has run. CodeIgniter makes this very easy. When a query runs it automatically saves some information about what happened (this applies to all db queries, not just AR). You can use that to your advantage to check if everything went as planned.

For the different types of queries you can check different values. Check out the query helper functions for more information.

A small list of the ones you could check depending on query type:
Code:
// INSERT
$this->db->insert_id();

// UPDATE and DELETE
$this->db->affected_rows();

// SELECT
$this->db->num_rows();

Syntax errors will trigger a CI database error to be shown, so you don't need to do anything to catch those.

Hope that helps.




Theme © iAndrew 2016 - Forum software by © MyBB