Re: DB_DataObject_FormBuilder: how to override processForm() method
| From: | Justin Patrin | Date: | Fri, 12 Nov 2004 18:12:23 +0000 |
| Subject: | Re: DB_DataObject_FormBuilder: how to override processForm() method | ||
| References: | 1 2 3 4 5 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-15475@lists.php.net to get a copy of this message | ||
On Fri, 12 Nov 2004 12:22:28 +0100, Giorgio Gorla <gmontag@tiscali.it> wrote:
> Justin Patrin ha scritto:
> > Sorry, I'm still not seeing what you're referring to. The API docs
> > don't say that you can put a processForm method in your DO....could
> > you give me a link and the approximate location?
> >
>
>
> http://pear.php.net/package/DB_DataObject_FormBuilder/docs/0.10.3/apidoc/DB_DataObject_FormBuilder-0.10.3/DB_DataObject_FormBuilder.html#methodprocessForm
>
Aha...it does say to override it. However, you should be overriding it
in a class extended from FB, not in the DO.
> > Then what is your problem? What do you mean by a "natural primary
> > key"? Shouldn't this just be one of your fields?
> >
> > If you're setting a special value for a key algorithmically, then you
> > can do it in preProcessForm(). Tell me what your code needs to do (or
> > show me your processForm that you wrote before) and I'll tell you how
> > to do it.
> >
>
> I have this table:
>
> CREATE TABLE
auth (
> username varchar(50) NOT NULL default '',
> password varchar(32) NOT NULL default '',
> PRIMARY KEY (username),
> KEY password (password)
> ) TYPE=MyISAM;
>
Why not this?
CREATE TABLE auth (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
username varchar(50) NOT NULL default '',
password varchar(32) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE (id),
UNIQUE (username),
KEY password (password)
) TYPE=MyISAM;
This way you have an auto-generated key *and* you have the unique
username constraint. Best of both worlds. In my experience, it's
always best to have an auto-generated key in a table. 1) because it
just makes things simpler and 2) it saves on space and processing time
when doing selects and when using foreign keys.
Also, why are you making password a key? Are you selecting by
password? If not, I don't see why it would be a key...I could
understand using the username as a key, but not the password...
> Within Auth.php (class DB_DataObject_Auth generated by createTable.php)
> I've set
>
> $fb_hidePrimaryKey = false
>
> in order to input primary key manually.
> But when I try to insert a new record, DBOF does an update.
>
Here's my take on this. If you want to use your natural primary key,
you should be using the forceQueryType() function of FB. If you call
as so:
$fb->forceQueryType(DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEINSERT);
The data will be inserted instead of updated. To check for this,
you'll have to do a query before the processform method is called,
then force the insert if it's a new record.
You could also change the way that the update() method on your DO
works something like this:
function update($do = false) {
if ($do === false) {
$checkDo = DB_DataObject::factory($this->__table);
if (!$checkDo->get('username', $this->username)) {
parent::insert();
return;
}
}
parent::update();
}
This will insert the record instead of updating it if the username
isn't found in the DB. This isn't a great system as you can't change
your username with it.
Ok, so that's a bad idea. Your best bet is to add a hidden field to
the form which tells you whether you'rs updating or inserting, then
force the query type on submit based on that value....of course, this
is precisely what having an auto-generate PK does for you. ;-)
--
Justin Patrin