当向多个表插入值时,应该采用什么方法?


What's the way to go when inserting values to multiple tables?

我插入一些值到employees表使用这个在控制器:

            $this->Employee->create();
            if ($this->CoreProgram->save($this->request->data)) {
                $this->Session->setFlash('Program has been added.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to add record.');
            }

然而,我想插入一条记录到另一个表audit。更具体地说,当前日期以及我用于employeesemployee_id字段。最好的方法是什么?我应该创建$this->Audit->create();吗?还是有更好的办法?

employees表上使用插入触发器,将审计条目插入到审计表中:

CREATE TRIGGER tr_employees_ins ON dbo.employees FOR INSERT AS  
BEGIN  
    INSERT dbo.audit( [op] ... employee_id ...)
    SELECT 'INS' ... employee_id
    FROM Inserted
END
相关文章: