Re: Another FormBuilder question
| From: | Justin Patrin | Date: | Tue, 15 Feb 2005 23:19:50 +0000 |
| Subject: | Re: Another FormBuilder question | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-17542@lists.php.net to get a copy of this message | ||
On Tue, 15 Feb 2005 16:15:58 -0500, James Stewart <james@jystewart.net> wrote:
> I've just been reworking some of the formbuilder code I was working on
> and asking questions about a couple of weeks ago and have run into a
> problem. I know I've encountered it before, but I can't remember how I
> got around it, which has been driving me crazy...
>
> I'm using the same function to build my form whether it's for editing a
> user or creating a new one. My code looks like (with relevant input
> validation elsewhere):
>
> $data = DB_DataObject::factory('users');
> if (!empty($_GET['id'])) {
> // I've tested and this is only happening when it ought to be
> $data->get($_GET['id']);
> }
> $ebuild =& DB_DataObject_FormBuilder::create($data);
> $form = $ebuild->getForm();
>
> if ($form->validate()) {
> $form->process(array(&$ebuild,'processForm'), false);
> return;
> }
> // Go on to output form
>
> and so on.
>
> The error I'm seeing is that when I create a new user the DataObject
> tries to insert it using an UPDATE query. ie:
>
> dataobjects_users: QUERY: UPDATE users SET user_name = 'James Stewart'
> , etc, etc, etc WHERE users.user_id = IS NULL
>
> Obviously this isn't working, but I can't remember why it's using
> UPDATE instead of INSERT in this instance.
>
What's your primary key for the table? If it's not an auto incremented
field, this is likely your problem. You can "get around this" by using
sequenceKey() on the DO to force your key to work. Check the archives
for more.
To really fix this I would suggest altering your table to have an auto
incremented primary key. You can still have a unique username and
such.
--
Justin Patrin