Maybe just before the save do :
$this->Foo->create();
On Sep 4, 8: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
-~----------~----~----~----~------~----~------~--~---