Francky - you rock!!!  The cacheQueries=false did the trick!  (I also
agree on the comments on how I was using exists() )

For what it is worth, here is what that section of code looks like now
- and works just the way I expect it to:  ;-)

/* Process the households db part. */
$this_userid = $csv_fields[7];
$match_userid = "Household.userid = '" . $this_userid . "'";
$household_id = $this->Household->field("Household.id",$match_userid);
if ($household_id) {
        $this->Household->id = $household_id;
} else {
        $this->Household->create();
        $this->data['Household']['last_name'] = $last_name;
        $this->data['Household']['HHID'] = $csv_fields[4];
       /* Fill in rest of data...  */
        $this->Household->save( $this->data);
        $household_id = $this->Household->getLastInsertID();
}

Thanks again!
Paul

On Aug 11, 6:38 pm, francky06l <[EMAIL PROTECTED]> wrote:
> As far as I know if you do not work with "transactional mode", you do
> not have anything to do for "updating" the database.
> If the insert gives you warning it's because you are trying to use an
> existing id.
> Set this->data['Household']['id'] = ""; before saving ..and set 
> $this->Household->cacheQueries = false;
>
> I did something like this in the past and never had problem. You
> should see INSERT when you insert a new record.
> Echo in LogError the result of the the field, also something like this
> is wrong to me :
>
>                                         $this->Household->id =
> $household_id;
>                                         if ($this->Household-
>
> >exists())
>
> The exists() call will return what you have set the line before.
>
> On Aug 12, 1:28 am, phalvrson <[EMAIL PROTECTED]> wrote:
>
> > Yup - I think you are right about using $this->id...  Adding the
> > Model::create() before the add prevents the update and forces an
> > insert (which gives warning messages about trying to insert a
> > duplicate record...)
>
> > I still need to figure out how to tell Model that I want saves
> > committed as they occur - not somewhere at the end of the loop or
> > function where I am assuming it is occurring now...
>
> > I suppose I could let the inserts give their error messages - they
> > don't show anymore when I set debug(0), but then I need to figure out
> > how to get the id from those records.
>
> > It does seem like if I see the INSERT for a record, then I should be
> > able to access that record with a SELECT...
>
> > On Aug 11, 5:57 pm, francky06l <[EMAIL PROTECTED]> wrote:
>
> > > Maybe a call to $this->Household->create() , before saving the new
> > > record.
> > > Actually if you look to the field methode (in the model.php core
> > > libs). it uses $this->id for reading.
> > > I think if you do not call the create() function of the model, you are
> > > always inserting over the same on.
>
> > > On Aug 12, 12:50 am, phalvrson <[EMAIL PROTECTED]> wrote:
>
> > > > Hey Francky, thanks for the reply.  I tried using your code, but had
> > > > the same results.  ( I think it is essentially the same since if I am
> > > > reading the code right, the $condition can be either a string or an
> > > > array...)
>
> > > > In further testing, if I am trying to find a match in something that
> > > > already existed in the database prior to this run, the Model::field()
> > > > finds it with no problem.  However, if I am trying to match with
> > > > something that I had inserted previously in this run, it does not find
> > > > it.
>
> > > > That leads me to believe I need to be doing some sort of commit or
> > > > something?  I am running with MySQL client version: 4.0.31-20070602.
> > > > I guess I was assuming that Model::save() would do whatever save was
> > > > necessary - but since I am running in a loop and adding in several
> > > > entries that I may want to access in a subsequent pass, is there
> > > > something I need to do to make that data available?
>
> > > > Thanks again for the info!
>
> > > > On Aug 11, 1:25 pm, francky06l <[EMAIL PROTECTED]> wrote:
>
> > > > > try to add :
>
> > > > >                             $match_userid = "Household.userid = '" .
> > > > > $this_userid . "'";
> > > > >                             $this->Household->set( array('id' =>
> > > > > $this_userid));
> > > > >                            $household_id = $this->HouseHold-
>
> > > > > >field("Household.id");
>
> > > > > hope this helps
>
> > > > > On Aug 11, 8:17 pm, phalvrson <[EMAIL PROTECTED]> wrote:
>
> > > > > > I thought I would start a new post since I think I understand the
> > > > > > problem better...
>
> > > > > > I am looping through a CSV file using fgetcsv and populating a set 
> > > > > > of
> > > > > > tables from it.
> > > > > > To illustrate, here is a trimmed version of my code to just show
> > > > > > processing for one of the tables, households:
>
> > > > > >         function process_csv_file()
> > > > > >         {
> > > > > >                 $checkFileFields = Array ( "id", "Item", "Qty", 
> > > > > > "Household", "HHID",
> > > > > > "FirstName", "LastName", "UserID", "Processed", "SchoolLocation",
> > > > > > "HouseholdEmail", "Address1", "Address2", "City", "State", "Zip",
> > > > > > "Reference#2" );
> > > > > >                 if (!empty($this->params['form'])) {
> > > > > >                        $file_handle = 
> > > > > > fopen($this->params['form']['File']
> > > > > > ['tmp_name'],"r");
> > > > > >                         $label_fields = fgetcsv ($file_handle);
> > > > > >                         if ($checkFileFields == $label_fields) {
> > > > > >                                 while (!feof($file_handle)) {
> > > > > >                                         $csv_fields = fgetcsv 
> > > > > > ($file_handle);
> > > > > >                                         /* Process the households 
> > > > > > db part. */
> > > > > >                                         $this_userid = 
> > > > > > $csv_fields[7];
> > > > > >                                         $match_userid = 
> > > > > > "Household.userid = '" . $this_userid . "'";
> > > > > >                                         $household_id = 
> > > > > > $this->Household->field("Household.id",
> > > > > > $match_userid);
> > > > > >                                         $this->Household->id = 
> > > > > > $household_id;
> > > > > >                                         if 
> > > > > > ($this->Household->exists()) {
> > > > > >                                                 /* Nothing to do - 
> > > > > > already have household id */
> > > > > >                                         } else {
> > > > > >                                                 
> > > > > > $this->data['Household']['HHID'] = $csv_fields[4];
> > > > > >                                            /* Fills in rest of 
> > > > > > data...
> > > > > > */
> > > > > >                                                 
> > > > > > $this->Household->save( $this->data);
> > > > > >                                                 $household_id = 
> > > > > > $this->Household->getLastInsertID();
> > > > > >                                                 
> > > > > > $this->Household->id = $household_id;
> > > > > >                                         }
> > > > > >                                 }
> > > > > >                                 fclose($file_handle);
> > > > > >                         } else {
> > > > > >                                 echo "Mismatched File";
> > > > > >                         }
> > > > > >                 }
> > > > > >         }
>
> > > > > > If this household record does not already exist, the following SQL
> > > > > > gets generated:
>
> > > > > > 7       SELECT `Household`.`id` FROM `households` AS `Household` 
> > > > > > WHERE
> > > > > > `Household`.`userid` = '115603' LIMIT 1
> > > > > > 8       INSERT INTO `households`
> > > > > > (`last_name`,`HHID`,`userid`,`email`,`address_1`,`city`,`state`,`zipcode`)
> > > > > > VALUES ('Miller',14821,'115603','[EMAIL PROTECTED]','123 Main
> > > > > > St','Laveen','AZ','85339')
> > > > > > 9       SELECT LAST_INSERT_ID() AS insertID
>
> > > > > > If this household record DOES already exist, no SQL is generated for
> > > > > > the select! And then it updates the first record!
>
> > > > > > so in the first case (doesn't exist), the line:
>
> > > > > > $household_id = 
> > > > > > $this->Household->field("Household.id",$match_userid);
>
> > > > > > causes this SQL to be generated:
>
> > > > > > SELECT `Household`.`id` FROM `households` AS `Household` WHERE
> > > > > > `Household`.`userid` = '115603' LIMIT 1
>
> > > > > > while in the second case (Already exists), no SQL is generated...
>
> > > > > > Or is it that the code that actually generates the SQL is the
> > > > > > "if ($this->Household->exists()) {"
> > > > > > which always returns false so that the else leg is executed in all
> > > > > > cases...
>
> > > > > > Can anyone please explain this behavior?  I think I must be missing
> > > > > > some crucial concept here!  I've spent a lot of time reading the 
> > > > > > code
> > > > > > for Model::field() and Model::find() and I'm just not getting it...
>
> > > > > > Thanks for your time,
> > > > > > Paul


--~--~---------~--~----~------------~-------~--~----~
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