为什么有些外键没有保存在 CakePHP 中

why some foreign keys are not saved in CakePHP

我正在使用 CakePHP 2.5.1 开发 Web 应用程序。我在将外键保存在数据库中并在网页中显示相关数据时遇到问题。我有用户、经理、客户、房屋、地址和电子邮件 table。

他们的关系是,

我的要求是,

现在,我可以保存所有数据了。但是在数据库中,没有保存外键,因此在显示关联数据时会产生问题。例如管理员注册完成后,在地址table中保存manager_id;但是 customer_id 和 house_id 仍然是 NULL。为经理添加客户时,它会在地址 table 中保存 customer_id;但是 manager_id 和 house_id 仍然是 NULL。但是,它应该保存 manager_id,因为客户属于经理。同样,当为客户添加房屋时,它会在地址 table 中保存 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')))

在用户控制器中,我使用了以下方法:

 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.')
            );
        }
     }

当 add() 在 userscontroller 中使用时,它使用外部 keys.i 保存用户和管理员数据。e manager_id 作为外键保存在 users table 中。但是类似的函数 addCustomer() 和 addHouse() 没有保存外键。客户 table 应将 manager_id 保存为外键,房屋 table 应将 customer_id 保存为外键。因为他们的关系就像——经理有很多客户有很多房子。为什么这些相似的功能在这里表现不同?谁能给我任何提示,为什么不保存外键?

您应该先将数据保存在您的父级 table 中,例如用户 table 像这样

$saveData = $this->data['User'];
$this->loadModel('User');
$this->User->save($saveData);

现在像这样获取它的主键:

$userId = $this->User->id;

现在将数据保存在依赖于“$userId”的其他 table 中意味着 $userId 现在是这些 table 的外键,例如地址、用户详细信息、经理等; 现在将数据保存在地址 table

$addressData = $this->data['Address'];
$addressData['user_id'] = $userId; // This be foreign Key for address table
$this->loadModel('Address');
$this->Address->save($addressData);

像这样你应该在依赖 tables
中保存数据 这个完整的代码应该在你想要保存数据的控制器函数中。