Part of the EllisLab Network
   
 
Active Record class update() return ?
Posted: 06 January 2008 12:53 PM   [ Ignore ]  
Research Assistant
RankRankRank
Total Posts:  663
Joined  08-26-2006

Hi,

Knows anybody the exact return values and types of the Active record class method update() ?

The User Guide does not mention anything.

The Docblock in the code pretends object.

My tests give me boolean TRUE, whether or not an update has been done (sort of strange, no ?).

Can there be other return values /types ?

I can of course get the affected rows, but still, this puzzles me.


Thanks.

 Signature 

Die Wirklichkeit ist das, was übrig bleibt, wenn man aufgehört hat, daran zu glauben.

Profile
 
 
Posted: 06 January 2008 01:19 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Rank
Total Posts:  51
Joined  04-17-2006

As far as I’m aware when you do an update all you would need to know is if it returns TRUE (a successful update), FALSE (one that is not successful) and get the number of affected rows. I don’t think it returns anything else or any other type.

Profile
 
 
Posted: 06 January 2008 01:28 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
RankRankRank
Total Posts:  663
Joined  08-26-2006

I’ve been looking into the code, update returns FALSE when one of its params is invalid, but besides that, it’ll return the result of the method query(), which is a pretty monster to read. It just puzzles me that it’ll return TRUE if no row has been updated.

 Signature 

Die Wirklichkeit ist das, was übrig bleibt, wenn man aufgehört hat, daran zu glauben.

Profile
 
 
Posted: 06 January 2008 04:51 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  550
Joined  10-18-2006

I don´t think is wrong returning TRUE if no row was updated… the query has been successfully executed, if you want to check the affected rows, affected_rows method will do the trick

 Signature 

Once in a while I remember I use Twitter

Profile
 
 
Posted: 06 January 2008 04:57 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  51
Joined  04-17-2006

tomcode:

Seppo would be correct. I just checked and as long as the query successfully gets executed (even if it doesn’t update anything), it will return TRUE.

Profile
 
 
Posted: 06 January 2008 05:08 PM   [ Ignore ]   [ # 5 ]  
Research Assistant
RankRankRank
Total Posts:  663
Joined  08-26-2006

Thanks a lot guys. I have it now fixed for me and it should be solid.

 Signature 

Die Wirklichkeit ist das, was übrig bleibt, wenn man aufgehört hat, daran zu glauben.

Profile
 
 
Posted: 07 January 2008 08:36 AM   [ Ignore ]   [ # 6 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3153
Joined  06-11-2007

Never has for me, if nothing is updated it doesnt return TRUE. Or it least thats how its always happened for me. Its hard to work out whats going on in that DB driver library with a headache :p

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 07 January 2008 10:18 AM   [ Ignore ]   [ # 7 ]  
Research Assistant
RankRankRank
Total Posts:  663
Joined  08-26-2006

Never has for me, if nothing is updated it doesnt return TRUE

@thepyromaniac: This is very strange. Can You give Your environnement ?
I’m working on WinXP under
Apache/2.2.3 (Win32) PHP/5.2.0 MySQL/5.0.22
Apache/1.3.33 (Win32) PHP/4.3.10 MySQL/3.23.49

I’ve checked again the code:

the update() method:
returns FALSE or exists (with $this->db_debug enabled) if one of its params aint valid.

else it ‘just’ returns the central query() method:

returns FALSE or exists (with $this->db_debug enabled) if one of its params aint valid.
returns $this->CACHE->read($sql) if cache is on
returns FALSE if $this->simple_query($sql) returns false
returns TRUE for write_type queries
returns TRUE if param $return_object (the third one) is other than TRUE
returns the created result object new ‘CI_DB_’.$this->dbdriver.‘_result’

That’s it.

The comment: (which omits to mention the third param)

<?php
    
/**
     * Execute the query
     *
     * Accepts an SQL string as input and returns a result object upon
     * successful execution of a "read" type query.  Returns boolean TRUE
     * upon successful execution of a "write" type query. Returns boolean
     * FALSE upon failure, and if the $db_debug variable is set to TRUE
     * will raise an error.
     *
     * @access    public
     * @param    string    An SQL query string
     * @param    array    An array of binding data
     * @return    mixed        
     */    
    
function query($sql$binds FALSE$return_object TRUE)
    

I get the behaviour as decribed in the comment above.

 Signature 

Die Wirklichkeit ist das, was übrig bleibt, wenn man aufgehört hat, daran zu glauben.

Profile