Re: Can't Get FormBuilder to do an INSERT
| From: | Jamie Alessio | Date: | Wed, 26 Jan 2005 22:07:17 +0000 |
| Subject: | Re: Can't Get FormBuilder to do an INSERT | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-17192@lists.php.net to get a copy of this message | ||
For future reference - with Justin's help I have come up with the solution to this problem. This was happening because I incorrectly specified my database links in the databaseName.links.ini file. I was specifying links like this:
-----------------------------------------------
[tableA]
tableB_id = tableB:tableB_id
[tableB]
tableB_id = tableA:tableB_id
-----------------------------------------------
This is incorrect. In this case, there is no need for the [tableB] relationship definition. Basically, you should never have the primary key of a table listed to the left side of the equal symbol in the links definition. To correct the above example you would change it to:
-----------------------------------------------
[tableA]
tableB_id = tableB:tableB_id
-----------------------------------------------
- Jamie
On Wed, 26 Jan 2005 11:01:23 -0800, Jamie AlessioI'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_FORCEUPDATE; if (!isset($this->_do->$pk) || !strlen($this->_do->$pk)) { $action = DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEINSERT; } ----------------------------------------------- The expected action is that this would evaluate to true and set $action = DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEINSERT but it never goes into that block and $action always remains equal to DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEUPDATE 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.Are you using release 0.11.0 or later? Are you sure that DB_DataObject set expertise_id to be a key? What happens if you print_r($do->keys());? print_r($do->sequenceKey());? How about print_r($fb->_getPrimaryKey($do));?