new feature additions to DB_DataObject_FormBuilder
| From: | Mocsnik Norbert | Date: | Mon, 08 Dec 2003 03:21:26 +0000 |
| Subject: | new feature additions to DB_DataObject_FormBuilder | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-24236@lists.php.net to get a copy of this message | ||
Hello,
I needed to get the inserted/updated record id somehow from the processForm() method, so I made some changes on my local copy of FormBuilder. (see the bottom of the mail for example usage)
Please let me know your thoughts and put it to the CVS version if you find this cool: The main idea is simple: let's have a $last_insert_update_id public variable inside FormBuilder with a default value of null, and set it to the inserted/updated record id in processForm(). All my modifications are marked in the source with "QLZADDED" (search for this string, without quotes):
http://www.e-ceg.hu/FormBuilder_addition2.phps
Okay, you probably don't have to time search for it, so here are my additions, for your convenience:
class DB_DataObject_FormBuilder
{
[...]
// QLZADDED
/**
* Contains the last id after insert/update.
* Be sure to look after this variable in your FormBuilder object and not in the
* form returned by DB_DataObject_FormBuilder::getForm().
*
* @access public
*/
var $last_insert_update_id = null;
// QLZADDED
[...]
function processForm($values)
{
[...]
if ($insert == true) {
$id = $this->_do->insert();
// BEGIN QLZADDED
$this->last_insert_update_id = $id;
// END QLZADDED
$this->debug("ID ($pk) of the new object: $id <br>\n");
} else {
$this->_do->update();
// BEGIN QLZADDED
$this->last_insert_update_id = $this->_do->$pk;
// END QLZADDED
$this->debug("Object updated.<br>\n");
}
[...]
}
[...]} ----------------- example usage of this improvement: This is useful for a couple of reasons .. let's say you you want to let the user upload a picture. When generating the form, simply add an <INPUT TYPE=FILE> element to the generated form before displaying it: $do = DB_DataObject::factory($this->tablename); $do->get($id); $fg =& DB_DataObject_FormBuilder::create($do); $form =& $fg->getForm($_SERVER['PHP_SELF'].'?t=store'); $pic = HTML_QuickForm::createElement('file', 'pic', 'Photo to upload'); $form->addElement($pic); echo $form->toHtml(); ..when handling file uploads do something like: $do = DB_DataObject::factory($this->tablename); $fg =& DB_DataObject_FormBuilder::create($do); $form =& $fg->getForm(); $pic = HTML_QuickForm::createElement('file', 'pic', 'Photo to upload'); $form->addElement($pic); // ***** SEE NOTE 1 HERE if ($form->validate()) {
$form->process(array(&$fg,'processForm'), false /*mergefiles*/ );
$pic = &$form->getElement('pic');
if ($pic->isUploadedFile()) {
$pic->moveUploadedFile($this->uploaddir, $fg->last_insert_update_id.'.jpg');
}
return true;
}
NOTE 1:
Remember that you have to add the 'pic' element manually again when handling the upload. This is because a brand new form will be generated here based on your $do, which (of course) won't contain the 'pic' element you've added to the form when displaying it. After adding this element here manually again, everything works as expected. However, this doesn't have to deal anything with my actual improvement, this is for the above example only.
Norbert Mocsnik