This is very helpful, but only solves half of my problem.

The problem is that my input data has duplicates.  For instance:

-----------------------------------------------------------------------------------------------

        function getFooId( $name )
        {
                $conditions = "`dbFoo.name` LIKE '%$name%'";
                $fooId = $this->Foo->field( 'id', $conditions );

                if( !isset($fooId) || $fooId == false ) {
                        $fooId = 0;
                }

                return $fooId;
        }

        function addFooToDb( $name, $text )
        {
                $retVal = 0;
                $fooId = $this->getFooId( $name );

                if( $fooId >= 0 )
                {
                        $newFoo['id'] = $fooId;
                        $newFoo['name'] = $name;
                        $newFoo['text'] = $text;

                        if( $this->Foo->save( $newFoo ) )
                        {
                                $retVal = $fooId;

                                if( $fooId == 0 ) {
                                        $retVal = $this->Foo->getLastInsertId();
                                }
                        }
                }

                return $retVal;
        }

-----------------------------------------------------------------------------------------------

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.

I'm trying to keep this as data independent as possible here, and
caching the name-to-id mappings in a hash of my own seems like an
unnecessary duplication of data, especially for a large data set.

Ideas?

-Packgrog

On Aug 31, 9:55 pm, "Chris Hartjes" <[EMAIL PROTECTED]> wrote:
> On 8/31/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>
>
> > Well, the query definitely runs, as the data exists in the table after
> > the script completes.  The problem is that I'm trying to access the
> > new row in the table during the same run of the script immediately
> > after running Model::save(), under the assumption that this command
> > would immediately save the result in the database.  Maybe it's just
> > that the model itself is caching the data and I need a better way of
> > forcing a read?
>
> There is no "caching of data" when it saves, and there is no command
> to "force reads". Whenever I've wanted the id of a record I've just
> saved, I've done the following:
>
> // assume $this->data has stuff in it
> if ($this->Foo->save($this->data)) {
>      $fooId = $this->Foo->getLastInsertId();
>
> }
>
> Then you go on your merry way doing whatever other work you wanted to
> do, happy in the knowledge that $fooId contains the id of the record
> you just created in the table associated with the Foo model.
>
> Hope that helps.
>
> --
> 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