I guess I'm being too vague here.  My original code is fine for doing
both inserts and updates.  Let me illustrate the problem with more
code:

              addFooToDb( 'Bob Jones', '04/05/2007 12:00:00' );
              addFooToDb( 'Sam Smith', '04/05/2007 12:01:24' );
              addFooToDb( 'Bob Jones', '04/05/2007 01:38:02' );

- CASE 1:  There is already a record in the Foo table where name =
"Bob Jones", but nothing else.

In this case, the final contents of Foo will be something like this:

        id      name    text
        1       'Bob Jones'     '04/05/2007 01:38:02'
        2       'Sam Smith'     '04/05/2007 12:01:24'

This is the behavior I'm expecting.  Sadly, this isn't always the
case...

- CASE 2:  Table Foo is empty during first run of the script.

In this case, the contents of Foo should look exactly the same as
above.  Instead, I'm seeing this:

        id      name    text
        1       'Bob Jones'     '04/05/2007 12:00:00'
        2       'Sam Smith'     '04/05/2007 12:01:24'
        3       'Bob Jones'     '04/05/2007 01:38:02'

This is entire due to the fact that even after a successful save for
the first record, getFooId( 'Bob Jones' ) will STILL RETURN '0',
rather than '1' as it should.  Again, getLastInsertId() isn't
immediately helpful in this case because we insert a 'Sam Smith'
record between the two 'Bob Jones' instances.

Does that clarify the problem?

-Packgrog

On Sep 4, 3:12 pm, "Chris Hartjes" <[EMAIL PROTECTED]> wrote:
> On 9/4/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>
>
> > 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.
>
> Well, that's what the example you had was doing anyway. :)  Here's a
> tweaked version.
>
> function addFooToDb($name, $ext) {
>               $retVal = 0;
>               $fooId = $this->getFooId( $name );
>
>               if( $fooId > 0 ) {
>                       $newFoo['Foo']['id'] = $fooId;
>               } else {
>                       $this->Foo->create();
>               }
>
>               $newFoo['Foo']['name'] = $name;
>               $newFoo['Foo']['text'] = $text;
>
>               if( $this->Foo->save( $newFoo ) ) {
>                    $retVal = $this->Foo->field('id');
>               }
>
>               return $retVal;
>       }
>
> That *should* create a new record if getFooId() returns zero, and
> update an existing record otherwise.
>
> --
> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to