我正在 CakePHP 2.5.1 中开发一个 Web 应用程序。我在数据库中保存外键并在网页中显示关联数据时遇到问题。我有用户、经理、客户、房屋、地址和电子邮件表。
他们的关系是,
- 一名经理可以拥有许多客户,
- 一个客户可以拥有很多房子,
- 一名经理可以有多个地址、多个电子邮件地址,
- 一个客户可以有多个地址、多个电子邮件地址,
- 一栋房子可以有多个地址
我的要求是,
- 管理者就是用户
- 注册后,经理将添加多个客户。针对每个客户,经理都会添加多个房屋。
- 每个用户/经理都有一个个人资料/索引页面,其中会列出其所有客户。
- 每个客户都有一个个人资料/索引页面,其中会列出其所有房屋。
现在,我可以保存所有数据了。但在数据库中,不保存外键,从而在显示关联数据时产生问题。例如,当经理注册完成后,它将manager_id保存在地址表中;但 customer_id 和 house_id 保持为 NULL。当为经理添加客户时,它会将 customer_id 保存在地址表中;但 manager_id 和 house_id 保持为 NULL。但是,它应该保存 manager_id,因为客户属于经理。同样,当为客户添加房屋时,会将 house_id 保存在地址表中;但 manager_id 和 customer_id 保持为 NULL。但是,它应该保存 manager_id 和 customer_id,因为房子属于客户,而该客户属于经理。
在AppController中,我定义了如下关系:
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authenticate' => array('Form' => array(
'contain' => array(
'Manager' => array(
'Address' , 'Email' , 'Customer' => array('House')))
在用户 Controller 中,我使用了以下方法:
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->saveAll($this->request->data, array('deep' => true))) {
$components = array('Session');
$this->Session->setFlash('Welcome!');
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(
__(' Please, try again.')
);
}
}
在 CustomersControllers 中,我使用了以下方法:
public function AddCustomer() {
if ($this->request->is('post')) {
$this->Customer->create();
if ($this->Customer->saveAll($this->request->data, array('deep' => true))) {
$primaryKey = $this->Customer->id;
$components = array('Session');
$this->Session->setFlash('Customer DATA have been saved!');
return $this->redirect(array('action' => ‘index'));
}
$this->Session->setFlash(
__('The Customer Data could not be saved. Please, try again.')
);
}
}
在 HousesControllers 中,我使用了以下方法:
public function AddHouse() {
if ($this->request->is('post')) {
$this->House->create();
$this->loadModel('House');
$houseData = $this->data['House'];
$houseData[‘house_id'] = $primaryKey;
if ($this->House->saveAll($houseData)) {
$components = array('Session');
$this->Session->setFlash(‘House DATA have been saved!');
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(
__('The House Data could not be saved. Please, try again.')
);
}
}
当在userscontroller中使用add()时,它会使用外键保存用户和经理数据。即manager_id作为外键保存在users表中。但类似的函数 addCustomer() 和 addHouse() 不保存外键。客户表应将 manager_id 保存为外键,房屋表应将 customer_id 保存为外键。因为他们的关系就像 - 经理有很多客户有很多房子。为什么这些相似的函数在这里表现不同?谁能给我任何提示,为什么外键没有保存?
最佳答案
您应该首先将数据保存在父表中,例如用户表 像这样
$saveData = $this->data['User'];
$this->loadModel('User');
$this->User->save($saveData);
现在像这样获取它的主键:
$userId = $this->User->id;
现在将数据保存在依赖于“$userId”的其他表中意味着$userId现在是这些表的外键,例如地址、用户详细信息、经理等;
现在将数据保存在地址表中
$addressData = $this->data['Address'];
$addressData['user_id'] = $userId; // This be foreign Key for address table
$this->loadModel('Address');
$this->Address->save($addressData);
像这样,您应该将数据保存在依赖表中
完整的代码应该位于您要保存数据的 Controller 函数中。
关于mysql - 为什么某些外键没有保存在 CakePHP 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35983515/