在我的cakephp3.0应用程序注册页面中,注册数据被插入到两个表中。我正在使用查询构建器来执行此操作。成功将数据插入第一个表(Projects表)之后,projectId(Projects表的主键是自动递增的值)将返回并用于插入到ProjectCustomers表的第二个表中,在projectCustomers表中projectId用作忘记密钥。我的控制器看起来像这样
public function initialize() {
parent::initialize();
$this->Project = new ProjectsTable();
$this->ProjectCustomer = new ProjectCustomersTable();
}
public function add() {
if ($this->request->is('post')) {
$project_id = $this->Project->putProject($formData['survey_id'], $formData['title'], $formData['operator'], $this->Auth->user('id'));
$this->ProjectCustomer->putProjectCustomer($project_id, $formData['air_id'], $formData['email'], $formData['name'], $formData['company_name'], $formData['department_name'], SYS_ADMIN, PROJECT_CUSTOMER_STATUS_ACTIVE, $date, $date);
return $this->redirect(['action' => 'view', $project_id]);
}
}
我需要的是我想在事务中进行这两个插入操作,以便无论如何第二次插入失败,应该回滚Projects表中的第一个插入。提前感谢您的帮助...