Almost there. I want addFooToDb() to either create a record if it
doesn't exist or update an existing record. What you provided here
will only update a record that's been found.
I should reiterate something with my example: The first time I run it
with the sample data, if there are no entries in the database table,
getFooId() will ALWAYS return 0, even if the entry has already been
successfully saved. After the script has been run, if I run it again
with the exact same data, it will always find the id for the first
record that was created.
Is that clearer at all?
-Packgrog
On Sep 4, 2:11 pm, "Chris Hartjes" <[EMAIL PROTECTED]> wrote:
> On 9/4/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Say I have an input file that takes in data that gets parsed into
> > $name and $text which I then pass to addFooToDb(). My uniqueness key
> > here is $name, and say we're assuming that the file is sorted by a
> > timestamp, and we want to overwrite existing entries with the same
> > name with any new ones that are encountered. Say our data looks
> > something like this:
> > -------------------------------------------------------------
>
> > "Bob Jones", "04/05/2007 12:00:00"
> > "Sam Smith", "04/05/2007 12:01:24"
> > "Bob Jones", "04/05/2007 01:38:02"
>
> > -------------------------------------------------------------
>
> > If there was no entry for "Bob Jones" when the loop starts, TWO
> > entries will be created, because each successive call to getFooId will
> > return '0' for the id, even though the entry has been created. The
> > getLastInsertId() method won't help here since it won't be the proper
> > reference once we add the "Sam Smith" record.
>
> Aha! I think I may understand what the problem is. You don't want to
> create new records, you want to simply update an existing one, right?
> If that's the case, then what you need to do is make sure that you are
> adding the primary key to your $data array and then executing a save.
> Let me show you an example using your own function with a few
> tweaks...
>
> function getFooId( $name )
> {
> $foo = $this->Foo->find("Foo.name LIKE '%$name%'");
>
> if ($foo) {
> return $foo['Foo']['id'];
> }
>
> return 0;
>
> }
>
> function addFooToDb($name, $ext) {
> $retVal = 0;
> $fooId = $this->getFooId( $name );
>
> if( $fooId > 0 )
> {
> $newFoo['Foo']['id'] = $fooId;
> $newFoo['Foo']['name'] = $name;
> $newFoo['Foo']['text'] = $text;
>
> if( $this->Foo->save( $newFoo ) )
> {
> $retVal = $this->Foo->field('id');
> }
> }
>
> return $retVal;
> }
>
> Try that out, that might fix the problem. My apologies if I have
> misunderstood the problem.
>
> --
> Chris Hartjes
> Senior Developer
> Cake Development Corporation
>
> My motto for 2007: "Just build it, damnit!"
>
> @TheBallpark -http://www.littlehart.net/attheballpark
> @TheKeyboard -http://www.littlehart.net/atthekeyboard
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---