| Jamie Alessio 2005-01-26, 3:57 pm |
| I'm having trouble getting DB_DataObject_FormBuilder to generate an
INSERT statement when creating new records. It always ends up generating
an UPDATE statement like this:
-----------------------------------------------
UPDATE expertise SET expertise = 'yeahyeah'
WHERE expertise.expertise_id = IS NULL
-----------------------------------------------
Here's some sample code that I am using to isolate this problem:
-----------------------------------------------
$obj = DB_DataObject::factory('Expertise');
$formBuilder = DB_DataObject_FormBuilder::create($obj);
$form = $formBuilder->getForm();
if($form->validate())
{
$form->process(array(&$formBuilder, 'processForm'), false);
echo "Should have done an insert, but it probably did an update.";
}
else
{
echo $form->toHtml();
}
-----------------------------------------------
This generates a form with these fields:
-----------------------------------------------
<input name="expertise_id" type="hidden" value="" />
<input name="submit" type="hidden" value="1" />
<input name="expertise" type="text" />
<input name="__submit__" value="Submit" type="submit" />
-----------------------------------------------
I'm using the latest CVS version of FormBuilder and have traced the
problem to this block of code in FormBuilder.php at line 2128
-----------------------------------------------
$action = DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEUPD
ATE;
if (!isset($this->_do->$pk) || !strlen($this->_do->$pk)) {
$action = DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEINS
ERT;
}
-----------------------------------------------
The expected action is that this would evaluate to true and set $action
= DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEINS
ERT but it never goes into
that block and $action always remains equal to
DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEUPD
ATE
Here's the dump of the the data in $this->_do->$pk at the time of the
if() statement in question
-----------------------------------------------
[expertise_id] => DB_DataObject_Cast Object
(
[type] => sql
[day] =>
[month] =>
[year] =>
[value] => NULL
)
-----------------------------------------------
According to the documentation for the FormBuilder.php processForm()
function "If the primary key is not set or NULL, it will be assumed that
you wish to insert a new element into the database, so DataObject's
insert() method is invoked." Should the primary key 'expertise_id' be a
'DB_DataObject_Cast Object' at this point? Could that be the problem? If
so, any thoughts on how/why it is getting cast that way?
What am I missing here? I feel like there must be something very basic
that I'm missing since the insert/update part of FormBuilder is one of
it's core features.
Thanks for any help.
- Jamie
|