SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 76 | Next

James Kennard

"Mastering Joomla! 1.5 Extension and Framework Development"

Next up is updating an
existing record. There are two ways of updating a record. We can insert the updated
data into the buffer and update the record. Alternatively, we can load the record,
insert the updated data into the buffer, and update the record. This example shows
how we implement the simpler first option:
// set values
$table->reset();
$table->setVar('id', $id);
$table->setVar('content', JRequest::getString('content'));
if ($table->check())
{
if (!$table->store())
{
// handle failed update
// use $table->getError() for an explanation
}
}
else
{
// handle invalid input
// use $table->getError() for an explanation
}
Although this works, if it fails, we do not even know whether it is due to an invalid
record ID or a more complex problem. There is a quirk we need to be aware of when
using the store() method. It only updates the values that are not null; we can
force it to update nulls, by passing a true parameter to the store method. The issue
with this is we would need to have the record loaded into the buffer so that we do
not overwrite anything with null values.


Pages:
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88