Re: DataObjects: Rows inserted in triplicate
| From: | Justin Patrin | Date: | Tue, 22 Feb 2005 23:52:28 +0000 |
| Subject: | Re: DataObjects: Rows inserted in triplicate | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-17633@lists.php.net to get a copy of this message | ||
On Tue, 22 Feb 2005 15:37:33 -0800, Sasha Sklar
<Sasha.Sklar@craveonline.com> wrote:
> Sorry I thought I had told you: we're using 4.0.23-standard.
>
> Justin, just to be clear, I'm using the generated classes now. I got rid
> of everything I manually created. The only thing that is written by me
> now is the test script and the database.ini file(listed below).
>
> Here's a full dump of my script, in case that helps:
>
> <?php
> require_once 'PEAR.php';
I suggest moving this up here. Additionally, you might try this line above it.
define('DB_DATAOBJECT_NO_OVERLOAD', 1);
> require_once 'DB/DataObject.php';
>
> $config = parse_ini_file('../DataObjects/data_objects.ini',TRUE);
> foreach($config as $class=>$values) {
> $options = &PEAR::getStaticProperty($class,'options');
> $options = $values;
> }
>
>
You shouldn't have to do this, see factory() usage below.
> require_once '../DataObjects/Test.php';
> ?>
> <!DOCTYPE html
> PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
>
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml"
> xml:lang="en" lang="en">
> <head>
> <title></title>
> <link rel="stylesheet" type="text/css" href=""
> media="" />
> <style type="text/css">
> <![CDATA[
> ]]>
> </style>
> <script type="text/javascript" src="" />
> <script type="text/javascript">
> <![CDATA[
> ]]>
> </script>
> </head>
> <body>
> <pre>
> <?php
> DB_DataObject::debugLevel (5);
>
> $test_do = new DO_test;
Instead of using new, it's more flexible to use factory().
$test_do = DB_DataObject::factory('test');
> $test_do->test_id = 'NULL';
Ok, this is certainly not right. DB_DataObject is going to try to save
the string NULL in the field....or it may try doing an SQL NULL, I
don't remember which. Either way, this isn't needed and is more likely
than not to screw something up. Just take out the above line. The auto
incrementing will work without it.
FYI, to do an SQL null it's best to do:
require_once('DB/DataObject/Cast.php');
$text_do->nullField = DB_DataObject_Cast::sql('NULL');
> $test_do->test_value = 'bork this shit';
> echo "testing 123<br />\n";
> $id = $test_do->insert();
insert() doesn't return the id, just so you know. This was changed a
long time ago. The id will automatically be set in the object after
the insert.
echo $test_do->test_id;
>
> DB_DataObject::debugLevel();
>
> if (PEAR::isError($id)) {
> die($id->getMessage());
> }
> echo $id;
>
> ?>
> </pre>
> </body>
> </html>
>
> As you can see, there is nothing special or complicated there.
Additionally, what version of DB, DB_DataObject, and PEAR are you using?
>
>
> -----Original Message-----
> From: Justin Patrin [mailto:papercrane@gmail.com]
> Sent: Tuesday, February 22, 2005 3:23 PM
> To: Sasha Sklar
> Cc: pear-general@lists.php.net
> Subject: Re: [PEAR] DataObjects: Rows inserted in triplicate
>
> On Tue, 22 Feb 2005 15:16:28 -0800, Sasha Sklar
> <Sasha.Sklar@craveonline.com> wrote:
> >
> > Justin, thanks for taking so much time with this.
> >
> > I tried a variety of things, from sending an email to echoing junk to
> > the screen. From all I can see, the function is only being called
> once.
> >
> > When you refer to the database.ini file, are you talking about this:
> > [DB_DataObject]
> >
> > database = BALETED
> > schema_location = /home/sasha/host/sasha/DataObjects
> > class_location = /home/sasha/host/sasha/DataObjects
> > require_prefix = DataObjects/
> > class_prefix = DO_
> >
> > Or are you referring to the schema? I didn't have a schema, because I
> > thought that manually generated classes didn't need it.
>
> I meant the schema file. You always need the schema file unless you're
> overriding the table() method of the class to return the schema and
> the keys() method to return the right array. If you don't have these
> you 1) have key/sequence problems, as you saw, and 2) all fields are
> assumed to be text. Putting in the types (the schema file) makes
> things work a lot better and more correctly. Plus this allow
> FormBuilder to work (check it out if you haven't ;-) ).
>
> > One was
> > generated along with the DO clases. Here's the entry for the test
> table:
> >
> > [test]
> > test_id = 129
> > test_value = 2
> >
> > [test__keys]
> > test_id = N
> >
> > Just so you know, I've actually used this library many times with
> ease.
> > I've always used manually generated files up till now and never had a
> > problem. It's just that I've started a new job and after extolling the
> > virtues of PEAR:DataObject to my new co-workers, I'm starting to sweat
> a
> > bit. I'm tempted to upload the files to my own private server, create
> a
> > copy of the table there, and see what happens. Is there anything else
> > you can think of that would be tripping things up? Any other
> information
> > I can give you?
>
> So what's the DB backend? Mysql? ODBC? Oracle?
>
> >
> > -----Original Message-----
> > From: Justin Patrin [mailto:papercrane@gmail.com]
> > Sent: Tuesday, February 22, 2005 2:14 PM
> > To: Sasha Sklar
> > Cc: pear-general@lists.php.net
> > Subject: Re: [PEAR] DataObjects: Rows inserted in triplicate
> >
> > On Tue, 22 Feb 2005 14:07:22 -0800, Sasha Sklar
> > <Sasha.Sklar@craveonline.com> wrote:
> > > OK, couple of things. I noticed an error in my manually created
> class.
> > I
> > > had copy/pasted the code for the staticGet function, and forgotten
> to
> > > change the name of the table.
> > >
> > > I ran the generator, and it worked fine. I tested the generated
> class,
> > > and it is doing the same thing, but it doesn't seem to be creating
> the
> > > seq table anymore.
> >
> > The seq table thing was a problem in your database.ini file, then. You
> > probably didn't set up the key value correctly when you created those
> > manually. Actually, your problem could well have been that you didn't
> > even have an ini file. ;-) Actually, this ini file is pretty hard to
> > manually generate, I'm surprised you got your code working at all.
> >
> > As for the multiple insert, try adding a mail() call to your code just
> > before the insert() call. Perhaps your script is being called multiple
> > times? Or it's in a loop or something? There really is no way that a
> > single insert can create multiple records unless your backend DB is
> > really screwed up.
> >
> > >
> > > -----Original Message-----
> > > From: Justin Patrin [mailto:papercrane@gmail.com]
> > > Sent: Tuesday, February 22, 2005 1:41 PM
> > > To: Sasha Sklar
> > > Cc: pear-general@lists.php.net
> > > Subject: Re: [PEAR] DataObjects: Rows inserted in triplicate
> > >
> > > On Tue, 22 Feb 2005 13:23:15 -0800, Sasha Sklar
> > > <Sasha.Sklar@craveonline.com> wrote:
> > > > Justin,
> > > >
> > > > Thanks for the info on running the generator. I'm going to give it
> a
> > > > try, although there is some resistance in my team to using the
> > > generated
> > > > classes. Not logical, I know, but that's work politics.
> > >
> > > The auto-gen classes are just as good as normal manual classes
> except
> > > they're automatic. :-) It only touches the auto-gen code, so as long
> > > as you put custom functions and such outside of the markers you're
> > > fine.
> > >
> > > >
> > > > We're using PHP 4.3.9. That is the entirety of the script, aside
> > from
> > > > some html for formatting and the configuration code at the top.
> I'll
> > > > list that and my config file:
> > > >
> > > <snip>
> > > > I've noticed a weird table in the database which I did not create.
> > It
> > > is
> > > > called test_seq. It contains a single row with a single field
> called
> > > id.
> > > > The id value is equal to the last id in test. If I remove the
> table
> > > and
> > > > run my script, it is regenerated.
> > > >
> > > > Any ideas?
> > >
> > > Ah....this is DB creating a sequence table because DB_DataObject
> > > thinks that this is a DB generated sequence. What DB backend are you
> > > using? Mysql? Is your ID set up as a auto incremented value? Do you
> > > have it set up as your primary key?
> > >
> > > >
> > > > -----Original Message-----
> > > > From: Justin Patrin [mailto:papercrane@gmail.com]
> > > > Sent: Tuesday, February 22, 2005 1:08 PM
> > > > To: Sasha Sklar
> > > > Cc: pear-general@lists.php.net
> > > > Subject: Re: [PEAR] DataObjects: Rows inserted in triplicate
> > > >
> > > > On Tue, 22 Feb 2005 12:33:52 -0800, Sasha Sklar
> > > > <Sasha.Sklar@craveonline.com> wrote:
> > > > > This is the issue: every insert I perform is generating 3 rows
> > > instead
> > > > > of 1.
> > > > >
> > > > > I'm using DataObjects with manually created classes, I can't use
> > the
> > > > > generator do to permission issues.
> > > > >
> > > >
> > > > What permissions issues? If you can't run it via CLI for some
> reason
> > > > you can always run the generator from the web. Just set the
> > > > permissions on the class dir (and files) to 777. Here's a simple
> > > > script I use to update some of my DOs from the web:
> > > >
> > > > <?php
> > > > $_SERVER['argv'][1] = '/path/to/DataObject.ini';
> > > > include('/path/to/PEAR/DB/DataObject/createTables.php');
> > > > system('chmod 777 /path/to/DataObjects/*');
> > > > ?>
> > > >
> > > > If you need more help, what's the output of the script you gave?
> Is
> > > > that the entirety of the script or is there more? What version of
> > PHP
> > > > are you using?
> > > >
> > >
> >
> > --
> > Justin Patrin
> >
> >
>
> --
> Justin Patrin
>
>
--
Justin Patrin