Re: Cannot retrieve new records after saving them
I realized after, when reading again, that you were talking about a loop. So the cacheQueries if affecting the getFoold. On Sep 4, 10:22 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > THAT'S IT! cacheQueries = false was exactly the detail I was > missing! Phew!!! :) > > Thanks very much everyone! Much appreciated! > > -Packgrog > > On Sep 4, 4:04 pm, francky06l <[EMAIL PROTECTED]> wrote: > > > I think you misunderstand, having !$foold will avoid setting the id, > > so it will call create and make a new record. If record exists, the > > create is not called and your id is set... it will update. > > Unless I am completely wrong concerning your problem, I think this > > code will update as well as create new record if needed. Now that you > > mention a "loop", using $this->Foo->cacheQueries = false might help. > > > On Sep 4, 9:50 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > > > > This is actually just a reiteration of the same code I already > > > posted. Setting $newFoo['Foo']['id'] = 0; will have the same effect > > > as create(); as it will simply create a new row. > > > > -Packgrog > > > > On Sep 4, 3:15 pm, francky06l <[EMAIL PROTECTED]> wrote: > > > > > I think with all the posts you get the answer, should look like : > > > > > 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 = ($foold ? $this->Foo->field('id') : > > > > $this->Foo->getLastInsertID()); > > > > } > > > > } > > > > >return $retVal; > > > > > On Sep 4, 9:03 pm, "[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. > > > > > > 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'][
Re: Cannot retrieve new records after saving them
THAT'S IT! cacheQueries = false was exactly the detail I was missing! Phew!!! :) Thanks very much everyone! Much appreciated! -Packgrog On Sep 4, 4:04 pm, francky06l <[EMAIL PROTECTED]> wrote: > I think you misunderstand, having !$foold will avoid setting the id, > so it will call create and make a new record. If record exists, the > create is not called and your id is set... it will update. > Unless I am completely wrong concerning your problem, I think this > code will update as well as create new record if needed. Now that you > mention a "loop", using $this->Foo->cacheQueries = false might help. > > On Sep 4, 9:50 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > > > This is actually just a reiteration of the same code I already > > posted. Setting $newFoo['Foo']['id'] = 0; will have the same effect > > as create(); as it will simply create a new row. > > > -Packgrog > > > On Sep 4, 3:15 pm, francky06l <[EMAIL PROTECTED]> wrote: > > > > I think with all the posts you get the answer, should look like : > > > > 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 = ($foold ? $this->Foo->field('id') : > > > $this->Foo->getLastInsertID()); > > > } > > > } > > > >return $retVal; > > > > On Sep 4, 9:03 pm, "[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. > > > > > 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;
Re: Cannot retrieve new records after saving them
On 9/4/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > 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 nametext > 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... There is no reason why the code I've provided shouldn't work, unless I've made such a glaring mistake that it shouldn't. Seems to me the method is 1) check to see if you have any records that match 'name' 2) if there is a match, get the id for that record 3) when you go to do a save, try setting $this->Foo->id = id of record to update 4) do your $this->Foo->save(...) and that should either create a new record or update an existing one Here's a stupid question for you: if getFooId is *always* returning a 0, then have you verified that getFooId is actually returning a correct value? That seems to me to be the only place where this chain breaks down: if getFooId doesn't return the proper value but returns 0 instead, then it simply isn't working. -- 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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
I think you misunderstand, having !$foold will avoid setting the id, so it will call create and make a new record. If record exists, the create is not called and your id is set... it will update. Unless I am completely wrong concerning your problem, I think this code will update as well as create new record if needed. Now that you mention a "loop", using $this->Foo->cacheQueries = false might help. On Sep 4, 9:50 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > This is actually just a reiteration of the same code I already > posted. Setting $newFoo['Foo']['id'] = 0; will have the same effect > as create(); as it will simply create a new row. > > -Packgrog > > On Sep 4, 3:15 pm, francky06l <[EMAIL PROTECTED]> wrote: > > > I think with all the posts you get the answer, should look like : > > > 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 = ($foold ? $this->Foo->field('id') : > > $this->Foo->getLastInsertID()); > > } > > } > > >return $retVal; > > > On Sep 4, 9:03 pm, "[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. > > > > 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 --~
Re: Cannot retrieve new records after saving them
This is actually just a reiteration of the same code I already posted. Setting $newFoo['Foo']['id'] = 0; will have the same effect as create(); as it will simply create a new row. -Packgrog On Sep 4, 3:15 pm, francky06l <[EMAIL PROTECTED]> wrote: > I think with all the posts you get the answer, should look like : > > 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 = ($foold ? $this->Foo->field('id') : > $this->Foo->getLastInsertID()); > } > } > >return $retVal; > > On Sep 4, 9:03 pm, "[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. > > > 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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
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 nametext 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 nametext 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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
I think with all the posts you get the answer, should look like : 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 = ($foold ? $this->Foo->field('id') : $this->Foo->getLastInsertID()); } } return $retVal; On Sep 4, 9:03 pm, "[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. > > 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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
Yep, sorry Chris, next time I will read more carefully the post. The name of the function does match the opposite of the functionality :-) On Sep 4, 8:32 pm, "Chris Hartjes" <[EMAIL PROTECTED]> wrote: > On 9/4/07, francky06l <[EMAIL PROTECTED]> wrote: > > > > > Maybe just before the save do : > > > $this->Foo->create(); > > Well, $this->Foo->create() will do exactly that: create a new record. > I think the object here is to update an existing record if it already > exists. > > -- > 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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
On 9/4/07, francky06l <[EMAIL PROTECTED]> wrote: > > Maybe just before the save do : > > $this->Foo->create(); > Well, $this->Foo->create() will do exactly that: create a new record. I think the object here is to update an existing record if it already exists. -- 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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
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? On Aug 31, 4:08 pm, Matt <[EMAIL PROTECTED]> wrote: > I find if you are saving a model it is always best to use save($this- > > >data->model); > > In my experience if you don't do this then you don't always save the > model. > > Have you tried using exit() right after the save, then checking your > table manualy to ensure the query ran? > > On Aug 31, 12:24 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > > > I'm working on a CakePHP script running from the command-line. I'm > > attempting to check for a record in table "foo", and if it doesn't > > exist, I add a new record using the Model::save() method. Afterwards, > > I immediately call Model::field() to try to capture the > > auto_incremented "id" field so that I can do extra work relating to > > that record. > > > Problem is, when I call $this->controller->Foo->field( 'id', > > $conditions ); immediately after the save, it returns null. In fact, > > this happens every time for that record until the script has completed > > and is run a second time. > > > I'm used to running scripts in Perl or TCL where when you make a > > database call, it completes immediately, so I'm confused by what seems > > to be some kind of caching of the data here. I can't seem to find any > > kind of flush() method to complete allow all of these things to be > > refreshed. Obviously I'm VERY new to CakePHP. Any insight here would > > be greatly appreciated. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
Hmmm... Conceptually, that'd be OK for the first time, but not exactly what I would be looking for when I used the same $this->Foo- >field('id', $cond); call for existing records that may have been updated. How would I save the output of getLastInsertId() to the 'id' value of the current instance of Foo so that $this->Foo->field('id', $cond); would return it properly (called from a different method)? On Aug 31, 4:06 pm, "John David Anderson (_psychic_)" <[EMAIL PROTECTED]> wrote: > On Aug 31, 2007, at 1:24 PM, [EMAIL PROTECTED] wrote: > > > > > I'm working on a CakePHP script running from the command-line. I'm > > attempting to check for a record in table "foo", and if it doesn't > > exist, I add a new record using the Model::save() method. Afterwards, > > I immediately call Model::field() to try to capture the > > auto_incremented "id" field so that I can do extra work relating to > > that record. > > > Problem is, when I call $this->controller->Foo->field( 'id', > > $conditions ); immediately after the save, it returns null. In fact, > > this happens every time for that record until the script has completed > > and is run a second time. > > Have you tried using getLastInsertId() rather than field()? > > -- John --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Cannot retrieve new records after saving them
On Aug 31, 2007, at 1:24 PM, [EMAIL PROTECTED] wrote: > > I'm working on a CakePHP script running from the command-line. I'm > attempting to check for a record in table "foo", and if it doesn't > exist, I add a new record using the Model::save() method. Afterwards, > I immediately call Model::field() to try to capture the > auto_incremented "id" field so that I can do extra work relating to > that record. > > Problem is, when I call $this->controller->Foo->field( 'id', > $conditions ); immediately after the save, it returns null. In fact, > this happens every time for that record until the script has completed > and is run a second time. Have you tried using getLastInsertId() rather than field()? -- John --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Cannot retrieve new records after saving them
I'm working on a CakePHP script running from the command-line. I'm attempting to check for a record in table "foo", and if it doesn't exist, I add a new record using the Model::save() method. Afterwards, I immediately call Model::field() to try to capture the auto_incremented "id" field so that I can do extra work relating to that record. Problem is, when I call $this->controller->Foo->field( 'id', $conditions ); immediately after the save, it returns null. In fact, this happens every time for that record until the script has completed and is run a second time. I'm used to running scripts in Perl or TCL where when you make a database call, it completes immediately, so I'm confused by what seems to be some kind of caching of the data here. I can't seem to find any kind of flush() method to complete allow all of these things to be refreshed. Obviously I'm VERY new to CakePHP. Any insight here would be greatly appreciated. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---