Yep, that's how I added a history to my last project.
E.g. to check if somebody changed his last_name (married? ;-)
DELIMITER //
create trigger contact_update
AFTER UPDATE ON contact
FOR EACH ROW
BEGIN
IF new.Last_Name != old.Last_Name THEN
INSERT INTO contact_history(`Contact_ID`,`Type`,`Action`)
VALUES(old.ID,'contact',concat('Last name of Contact changed from
',old.Last_Name,' to ',new.Last_Name));
end if;
END;//
In the view for contacts I add an info-box where i scrawl the
contact_history table for related entries and can see per contact how
is data changed during time.
But be cautios using trigges. You may easily run into loops or
conflicts (one trigger triggers another one and if a table is affected
by both the trigger fails!!)
Good luck!
Ralph
On 20 Aug., 13:21, RichardAtHome <[EMAIL PROTECTED]> wrote:
> If you are using a later version of MySQL (>= 5.0.2), you could add a
> database trigger (ON UPDATE, ON DELETE, etc.) to log changes
> automatically without having to change your code.
>
> See:http://dev.mysql.com/doc/refman/5.0/en/triggers.html
>
> On Aug 20, 11:59 am, <[EMAIL PROTECTED]> wrote:
>
> > This my attempt to solve the problem (logfile is a table for auditing):
>
> > function beforeSave() {
> > $this->currentAction = $this->id ? 'Update' : 'Insert';
> > return true;
> > }
>
> > function afterSave() {
> > // hope that this is not necessary
> > // because cake has filled in the id after save
>
> > if(!$this->id) {
> > $id = $this->getLastInsertId();
> > } else {
> > $id = $this->id ;
> > }
>
> > /*
> > ** Save Log
> > **
> > */
> > loadModel("Logfile");
> > $Logfile = new Logfile;
> > $data['Logfile']['created'] = date("Y-m-d H:i:s");
> > $data['Logfile']['tablename'] = $this->name;
> > $data['Logfile']['tableid'] = $this->getLastUsedId();
> > $data['Logfile']['username'] = @$_SESSION['cakeAuth']['login'];
> > $data['Logfile']['action'] = $this->currentAction;
> > $data['Logfile']['ipaddress'] = @$_SERVER['REMOTE_ADDR'];
> > $Logfile->save($data);
> > $Logfile = null;
>
> > $this->currentAction = '';
>
> > return true;
> > }
>
> > function afterDelete() {
> > /*
> > ** save Log
> > */
> > loadModel("Logfile");
> > $Logfile = new Logfile;
> > $data['Logfile']['created'] = date("Y-m-d H:i:s");
> > $data['Logfile']['tablename'] = $this->name;
> > $data['Logfile']['tableid'] = $this->getLastUsedId();
> > $data['Logfile']['username'] = @$_SESSION['cakeAuth']['login'];
> > $data['Logfile']['action'] = 'Delete';
> > $data['Logfile']['ipaddress'] = @$_SERVER['REMOTE_ADDR'];
> > $Logfile->save($data);
> > $Logfile = null;
>
> > return true;
> > }
>
> > > -----Messaggio originale-----
> > > Da: [email protected]
> > > [mailto:[EMAIL PROTECTED] Per conto di zipman
> > > Inviato: lunedì 20 agosto 2007 12.42
> > > A: Cake PHP
> > > Oggetto: Auditing
>
> > > Hello,
>
> > > I have finished a project and now I want to add auditing
> > > support, as to know when an update or delete occured and with
> > > what parameters.
> > > I guess the solution to my problem is the afterSave callback
> > > which I'll have to put in the app_model.php. The problem is I
> > > don't know how to access the parameters used for save (the
> > > model on which the save function was called and with what
> > > parameters). How can I do this?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---