->update()

->update() – Update objects variables into database

Synopsis

int $DB_DataObject->update ( dataobject|boolean $original|$useWhere )

Description

Updates current objects variables into the database. if you supply it with a dataObject, as an argument, it will only update the differences between the new and old.

if called with DB_DATAOBJECT_WHEREADD_ONLY as the argument, the update request is built based on the whereAdd values, rather than the primary key. This enables global updates to be performed, rather than single row ones.

Parameter

  • DataObject $original - if provided the update query will be built from the difference between the current and original dataobject.

Return value

int number of rows affected or FALSE on failure

Throws

Possible PEAR_Error values
Error code Error message Meaning Solution
DB_DATAOBJECT_ERROR_INVALIDCONFIG "update:No table definition for $table"    
DB_DATAOBJECT_ERROR_NODATA "update: No Data specifed for query $settings"    

Note

This function can not be called statically.

Example

Simple fetch and update

<?php
$person 
= new DataObjects_Person;
$person->get(12);
$person->name='fred';
$person->update();     

$person = new DataObjects_Person;
$person->get(12);
$original = clone($person); // clone is emulated in php4 for compatibility reasons. 
$person->name='fred';
$person->update($original);
?>

Resulting SQL

        
SELECT * FROM person WHERE id = 12
UPDATE person SET name='fred', age='21', eyes='blue' WHERE id = 12

SELECT * FROM person WHERE id = 12
UPDATE person SET name='fred'  WHERE id = 12

Simple fetch and update

<?php
$person 
= new DataObjects_Person;
$person->removed=1;
$person->whereAdd('age > 21');
$person->update();
?>

Resulting SQL

        
SELECT * FROM person WHERE id = 12;
UPDATE person SET removed=1 WHERE age > 21
Insert current objects variables into database (Previous) Delete items from table (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: jacek
interesting case:

$person = new DataObjects_Person;
$person -> get(1);
$person -> name = 'fred';
$person -> update();

works but:

$person = new DataObjects_Person;
$person -> get(0);
$person -> name = 'fred';
$person -> update();

doesn't - (and apparently this is not a BUG - I tried to file bug report)

the table has only to records:

id, name
0 john
1 joe

id column is the primary key.





Note by: mike@mikebrittain.com
The second code example is missing the DB_DATAOBJECT_WHEREADD_ONLY parameter which allows for running update queries based only on whereAdd values and not by primary key. See description for this method (above).

$person = new DataObjects_Person;
$person->removed=1;
$person->whereAdd('age > 21');
$person->update(DB_DATAOBJECT_WHEREADD_ONLY);
Note by: evan@prodromou.name
update() will silently skip any columns that are keys -- even if they're just unique keys, not primary keys.

Say you have a table like this:

CREATE TABLE person (id integer primary key, name varchar(255), email varchar(255) unique key)

This is a pretty common pattern.

The following code will do nothing:

$p = Person::staticGet(1);
$orig = clone($p);
$p->email = 'newemail@example.com';
$result = $p->update($orig);

There doesn't seem to be a workaround for it -- I've resorted to updating unique key columns by hand.

It would be nice if the code for skipping key columns differentiated between primary keys and unique keys and let you update the latter.
Note by: pierre2543@hotmail.com
If you want to update a field to null in the database set the field in your DO to the string 'null'.

For string fields, setting it to '' or 'null' issues null in the update statement.

Example:
Set the contractNumber to null.

//INCORRECT
$contract->get(123);
$contract->contractNumber = null;
$contract->update();

//CORRECT
$contract->get(123);
$contract->contractNumber = 'null';
$contract->update();
Note by: user@example.com
When called with

$object->update($original);

this function appears to return true rather than 0 if no rows are affected.