Re: DB_DataObject: auto-convert YYYYMMDD integers to proper mySQL DATE types

From: Date: Wed, 23 Mar 2005 18:16:56 +0000
Subject: Re: DB_DataObject: auto-convert YYYYMMDD integers to proper mySQL DATE types
References: 1 2 3 4 5 6 7 8  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-18292@lists.php.net to get a copy of this message
On Wed, 23 Mar 2005 17:37:31 +0100, Massimiliano Bariola <m.bariola@prodigiweb.it> wrote: > Hi, > > I have written a script to batch-process a rather large dump from a > non-mysql database and feed it to my mysql server. > > from the manual of mySQL section 11.3.1, it says that I can insert > values into DATE column types if my original datum is: > > "As a number (or string) in either |YYYYMMDD| or |YYMMDD| format, > provided that the number (or string) makes sense as a date. For example, > |19830905| and |830905| are interpreted as |'1983-09-05'|.". > > indeed, if I raw-query it, it works. so, being in need of making many > different concversions on the data rows, I tried to set up a > DB_DataObject which will hold the logic for the batch conversion. > > the relevant snippet is as follows: > > $anag = new DataObjects_Tb_anagrafica(); > echo $anag->find(); > while($anag->fetch()) { > // copies the numeric fields into the relevant mySQL DATE fields, > using the above mentioned conversion trick > //TDataNascita, etc. are temporary mySQL DATE fields which will hold > the properly-formatted dates > $anag->TDataNascita = $anag->DataNascita; > $anag->TDataModifica = $anag->DataModifica; > $anag->TDataCreazione = $anag->DataCreazione; > $anag->update(); > } > > Alas, no conversion is done. If I change one of the above lines like > $anag->TDataNascita = $anag->'2005-03-23'; // a sample date in > string format > > then the field WILL get properly updated. > > So I set up a workaround like this; which seems to work: > > $anag->TDataNascita = ''.$anag->DataNascita; //prepend an empty > string to cast it all to string > > maybe this is a non - compliance of the current DB_DataObject? or am I > doing something wrong? I currently have the 1.7.12 version installed. > The problem is likely that it's coming through as a number and DB_DO is seeing it as a number. Doing ''.$anag->DataNascita makes it a string. It would be better to use (string)($anag->DataNascita). I know this is longer, but it states your intentions better. You can also insert dates as "YYYY-MM-DD HH:II:SS" into mysql, and I prefer these. I would suggest you use this format for inserting if you can. Note: Mysql TIMESTAMPs are in YYYYMMDDHHIISS format, which is more like what you're describing. -- Justin Patrin

« previous php.pear.general (#18292) next »