DB_DataObject producing incorrect SQL
| From: | Michael Henry | Date: | Mon, 13 Nov 2006 01:39:15 +0000 |
| Subject: | DB_DataObject producing incorrect SQL | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-25795@lists.php.net to get a copy of this message | ||
Hi all,
I've just recently started using PHP so it is entirely possible that I have missed something obvious but I am finding that DB_DataObject is producing an incorrect "where" clause when I try to perform an update.
I'm using DB_DataObject to update a table (which happens to be my Translations2 table. The Translations2 code is not involved here however.) My code is almost identical to example 33-1 in the DB_DataObject::update() documentation:
$do = DB_DataObject::factory ('mlci.translations');
if (PEAR::isError($do)) {
throw new Exception('Unable to retrieve translations');
}
// The lang and string_id fields uniquely identify a row
$do->lang = $lang;
$do->string_id = $stringID;
$count = $do->find(true);
// ASSERT: $count == 1
if ($count == 1) {
$original = clone($do);
$do->translation = $value;
$do->update($original);
if (PEAR::isError($do)) {
throw new Exception("Failed to update translation<BR>" .
"Lang: {$lang} String ID: {$stringID} Value: {$value}<BR>" .
$do->getMessage());
} else {
$updates .= "Lang: {$lang} String ID: {$stringID} Value:
{$value}\n";
}} else {
throw new Exception('Translation not found in the database');
}
Obviously $lang, $stringID, and $value were defined before this block; their values are 'to', 209, and 'asdfasdf', respectively.
The one difference between my code and the example is that I am using find() instead of get(). This may be related to the problem but from what I can tell the behaviour should be the same. The resulting SQL to perform the update is incorrect:
UPDATE mlci.translations
SET translation = 'asdfasdf'
WHERE ( mlci.translations.id = 104 )
AND ( mlci.translations.string_id = 209 )
AND ( mlci.translations.page = 'add_org' )
AND ( mlci.translations.lang = 'to' )
AND ( mlci.translations.translation = 'asdfasdf' )
It is using the value of $do->translation in the where clause instead of $original->translation. (To be honest I would expect the package to realise that "id" is the primary key and not bother with any of the other conditions but that is neither here nor there.)
If anyone can shed some light on what I'm doing wrong I'd be very grateful.
Regards,
Michael