->update() -- オブジェクト変数でデータベースを更新する
説明
現在のオブジェクト変数でデータベースを更新します。
もしデータオブジェクトを引数として提供した場合、
新旧データの間での違いだけが更新されます。
引数に DB_DATAOBJECT_WHEREADD_ONLY のみを指定してコールした場合は、
主キーではなく whereAdd の値に基づいてクエリが構築されます。
これを使用すると、一行ずつの更新ではなく複数レコードの一括更新が可能となります。
返り値
int 作用した行数、あるいは失敗した場合 FALSE
例外・エラー
表 39-1PEAR_Error の値
| エラーコード | エラーメッセージ | 意味 | 解決策 |
|---|
| DB_DATAOBJECT_ERROR_INVALIDCONFIG | "更新:$table の定義がありません" | | |
| DB_DATAOBJECT_ERROR_NODATA | "更新:クエリ $settings にデータが指定されていません" | | |
注意
この関数は、スタティックにコールする
ことはできません。
例
例 39-1単純なフェッチと更新
<?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);
?>
|
|
例 39-2結果の 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 |
|
例 39-3単純なフェッチと更新
<?php
$person = new DataObjects_Person;
$person->removed=1;
$person->whereAdd('age > 21');
$person->update();
?>
|
|
例 39-4結果の SQL SELECT * FROM person WHERE id = 12;
UPDATE person SET removed=1 WHERE age > 21 |
|
|
->insert() (Previous)
|
(Next) ->delete()
|
|
|
Download Documentation
|
Last updated: Sun, 07 Sep 2008 |
|
Do you think that something on this page is wrong? Please file a bug report or add a note.
|
| User Notes: |
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.
|
|