Re: 2 newbie Qs: DB_DataObjects
| From: | Justin Patrin | Date: | Wed, 15 Jun 2005 16:38:18 +0000 |
| Subject: | Re: 2 newbie Qs: DB_DataObjects | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-19638@lists.php.net to get a copy of this message | ||
On 6/15/05, C.J. Mackie <cjmackie@princeton.edu> wrote:
> I'm setting up DB_DO on PHP5/MySQL4. I've got most things working, but I've
> run into a couple of quirks on which I could use some help.
>
> 1) is it possible to force DB_DO to use PEAR sequences for inserts to MySQL?
> The docs say it uses PEAR sequences for every db except MySQL. I'm using
> PEAR sequences already, and need to continue doing so. I can always get the
> sequence #s and add them manually, but it seems more elegant to ask DB_DO to
> use them itself, if possible.
Yes it will do this on Mysql. All you need to do is set up that field
as your Primary Key but *not* auto_increment. DB_DO should
automatically use a DB sequence.
>
> 2) Everything else seems to be working fine except for date parsing, which
> isn't working at all. I try to insert a record that contains a 'CreateDate'
> field, and MySQL inserts a default (0000-00-00 00:00:00, to be precise).
>
> I've tried formatting the correct insert string many different ways, and
> I've tried passing through the NOW() function, but neither works. Here's
> the example code:
AFAIK DB_DO doesn't do any date manging on its own. Setting your date
field to the correct format (YYYY-MM-DD HH:II:SS) should work
perfectly. However if you want to use NOW() you can use the Cast
object.
$u->CreateDate = DB_DataObject_Cast::sql('NOW()');
>
>
> $u = DB_DataObject::Factory('users');
> $testID = $dbh->nextID('users');
> $u->ID = $testID;
>
> ... fill in other fields here ...
>
> $my_timestamp = strftime("%Y-%m-%d %H:%M:%S");
> $u->CreateDate = $my_timestamp;
> $u->insert();
> $u->update();
Why are you doing an update here? I assume you think it updates the
variables in the DO but this is not what it does. update() runs an
UPDATE query to update the data in the record. There is no reason
whatsoever to run an update after an insert unless you changed the
data.
>
> Can anyone tell me what I'm doing wrong? (n.b., I've looked at the
> 'DB_DO::Cast' objects as a solution, but I'm not allowed to use code labeled
> as 'beta'.)
"Not allowed"? Cast is pretty stable, in fact I don't think it's
changed for quite a while. Why do you say it's beta? I would also
suggest you take a look at DB_DO_FormBuilder but it's still beta
(Mostly because I haven't had time to create proper unit tests and I
keep changing little things in the API as I find possible problems).
It's a great help, though, and I'm always using it myself in
production so any error which pop up I tend to fix quickly.
>
> Many thanks, and apologies in advance if I missed a FAQ somewhere, --CJ
>
--
Justin Patrin