Re: Twisting a little bit afterSave() use...
Hello! Many thanks to Mariano and Wood for your inputs. You know, I'm having a little trouble making the function afterSave() { if (!isset($this-data[$this-name]['id']) || empty($this-data[$this-name]['id'])) { // We just created the record } } solution work. It always executes the code inside the if(), no matter if you're creating the object or just editing it. It's weird... Anyway, I'm going to try Wood solution by now and report back later on. Thanks again. --~--~-~--~~~---~--~~ 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: Twisting a little bit afterSave() use...
Hello again!! I think I've found a way to solve my problem... I do the table creation before saving (using beforeSave() as opposed to afterSaave()) and use the $this-exists() method to check wheter or not my Model instance already exists... I think it's pretty straightforward...but I might be poisoned by thinking about this some much time... :-) Anyway, this is just for prototyping effects, for the app first version...later on, me or some other team member will code a proper solution... I leave here the code stub, in case anyone else might have the same problem in the future... function beforeSave() { // Checking if the instance exists... if (! $this-exists()) { // Create the table } return true; } Thanks again to everybody! Cheers. --~--~-~--~~~---~--~~ 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: Twisting a little bit afterSave() use...
Isn't the choice between add/edit determined by whether the Model's id field is set? Could you not look at the Cake source to see how they decide if it's an add or edit, and copy it? Wood On Feb 19, 7:11 pm, mindcharger [EMAIL PROTECTED] wrote: Hello! Here's the thing, I'm a couple of weeks from deploying my application and for this first version we will be using scaffolding for a lesser- used system part...so the 'quit scaffolding and code it by hand' is not an option (at least for a timely delivery...). In my system, when you create a new Entity you need not only to add a record on the entities table but also to create a family of Entity_X tables (where X is a name suffix...). My idea was to use the afterSave() callback to create the Entity_X's tables AFTER the record is inserted. Thing is the afterSave() is used also when you edit a model (and when you edit an Entity you don't want to create new tables, but if you might need to alter the 'X' part of the Entity_X tables...) so I must make the Model somehow aware of the controller action that was requested. As far as I know this Model awareness of the controller is not possible with cake, right? Does anybody can help me out on these one? Thanks in advance! --~--~-~--~~~---~--~~ 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: Twisting a little bit afterSave() use...
On your afterSave() you can check existence of $this-data[$this-name]['id']. If you look at cake/libs/model/model_php5.php for save() you see: if (!empty($this-id)) { // ... } else { if ($db-create($this, $fields, $values)) { $this-__insertID = $db-lastInsertId($this-tablePrefix . $this-table, $this-primaryKey); if (!$this-__insertID $newID != null) { $this-__insertID = $newID; $this-id = $newID; } else { $this-id = $this-__insertID; } // ... $this-afterSave(); $this-data = false; // ... } } So checking for $this-id wouldn't be a good option since save() function fills it out for you, but $this-data won't be altered, so you can do: function afterSave() { if (!isset($this-data[$this-name]['id']) || empty($this-data[$this-name]['id'])) { // We just created the record } } -MI --- Remember, smart coders answer ten questions for every question they ask. So be smart, be cool, and share your knowledge. BAKE ON! blog: http://www.MarianoIglesias.com.ar -Mensaje original- De: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] En nombre de mindcharger Enviado el: Lunes, 19 de Febrero de 2007 04:11 p.m. Para: Cake PHP Asunto: Twisting a little bit afterSave() use... Hello! Here's the thing, I'm a couple of weeks from deploying my application and for this first version we will be using scaffolding for a lesser- used system part...so the 'quit scaffolding and code it by hand' is not an option (at least for a timely delivery...). In my system, when you create a new Entity you need not only to add a record on the entities table but also to create a family of Entity_X tables (where X is a name suffix...). My idea was to use the afterSave() callback to create the Entity_X's tables AFTER the record is inserted. Thing is the afterSave() is used also when you edit a model (and when you edit an Entity you don't want to create new tables, but if you might need to alter the 'X' part of the Entity_X tables...) so I must make the Model somehow aware of the controller action that was requested. As far as I know this Model awareness of the controller is not possible with cake, right? Does anybody can help me out on these one? Thanks in advance! __ InformaciĆ³n de NOD32, revisiĆ³n 2070 (20070219) __ Este mensaje ha sido analizado con NOD32 antivirus system http://www.nod32.com --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---