蛋糕PHP保存空模型


CakePHP save empty model

我有两种类型的用户:employeescustomers,我需要区分它们,所以我为它们创建了 2 个单独的表。然后我选择了CakePHP作为我的框架,然后我想遵循简单身份验证教程,其中有一个用户表。所以我决定创建这样的表:

CREATE TABLE IF NOT EXISTS `users` (       
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `username` varchar(36) COLLATE utf8_czech_ci NOT NULL, 
  `password` varchar(36) COLLATE utf8_czech_ci NOT NULL, 
  `role` varchar(30) COLLATE utf8_czech_ci NOT NULL,
  `name` varchar(30) COLLATE utf8_czech_ci NOT NULL,
  `surname` varchar(40) COLLATE utf8_czech_ci NOT NULL,
  `phone` varchar(16) COLLATE utf8_czech_ci NOT NULL,
  `email` varchar(40) COLLATE utf8_czech_ci NOT NULL,
  `employee_id` int(20) DEFAULT NULL,
  `customer_id` int(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `EMAIL` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `employee` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `customer` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT AUTO_INCREMENT=1 ;

我的模型:

员工

public $hasOne = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'employee_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

用户:

    public $belongsTo = array(
            'Employee' => array(
                'className' => 'Employee',
                'foreignKey' => 'employee_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            );

添加用户功能:

public function add() {
    if ($this->request->is('post')) {      
      $this->User->create();    
      $roles = array('admin', 'employee');
      if (in_array($this->request->data['User']['role'], $roles)) {                                                 
        if ($this->User->Employee->save($this->request->data))
          $this->request->data['User']['employee_id'] = $this->User->Employee->getLastInsertId();
        else {
          $this->Session->setFlash(__('Employee could not be saved.'));
          return;
        }
      }
      else {
        $this->User->Customer->save($this->request->data);
        $this->request->data['User']['customer_id'] = $this->User->Customer->getLastInsertId();          
        $this->User->Customer->create();   
      }                   
      if (!$this->User->save($this->request->data)) {
        $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
      }
      else {          
       $this->Session->setFlash(__('The user has been saved.'));
      } 
      //return $this->redirect(array('action' => 'index'));           
    }
    $employees = $this->User->Employee->find('list');
    $customers = $this->User->Customer->find('list');
    $this->set(compact('employees', 'customers'));
}

我有一种感觉,这个概念模型是不对的,因为EmployeeCustomer表只包含主键。

另外 ($this->User->Employee->save($this->request->data) 返回false。是否存在 CakePHP 无法保存空模型的问题?

或者您有更好的想法如何对这些表进行建模吗?

谢谢。

如果您打算获得特定于员工类型用户和客户类型用户的信息,则您的前进方向很好,您可以将这些未来字段添加到客户和员工表中。 如果您需要做的就是区分员工类型用户和客户类型用户,那么您只需要在用户表中有一个字段来区分类型,例如

is_employee tinyint(1) default 0,