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: [email protected] [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 [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