Cakephp在插入时连接两个模型


Cakephp joining two models on insert

OK,所以我有两个表EmployeeUser

我的员工模型是这样的:

    class Employee extends  AppModel{
    public $name = 'Employee';
    public $primaryKey = "employee_id";
    public $actsAs = array('Containable');
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'dependent' => false,
            'foreignKey' => 'user_id'
        )
    );
}

我的用户模型是这样的:

    App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
// ...
    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }

    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('employee', 'client')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        )
    );
}

在我的员工控制器中,我有一个允许员工添加其他员工的操作,操作如下:

    public function add() {
    if ($this->request->is('post')) {
        $this->Employee->User->create();
        if ($this->Employee->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

我的员工表看起来像这个

employee_id user_id

现在,每当我添加用户时,用户都会正确地添加到我的用户表中,并且一行也会添加到employee表中,但employee表中有两个错误:

  1. employee_id是一个自动递增,但这并没有发生,而且它似乎一直在等待1。(这样我尝试创建的每个用户都是employee_id=1)

  2. CCD_ 7总是0,然而在用户表中user_。

有人能告诉我为什么会发生这种情况,以及我该如何解决吗?

更新

我在员工控制器中的添加操作现在看起来是这样的:

   public function add() {
    if ($this->request->is('post')) {
        $this->Employee->User->create();
        if ($this->Employee->User->saveAll($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

我在我的用户模型中添加了一个hasMany:

public $hasMany = array(
    'Employee' =>array(
        'className' => 'Employee',
        'dependent' => true,
        'foreignKey' => 'user_id'
    )
);

仍然没有变化

一些问题。。。

1) employees表的主键应该是id,而不是employee_id。根据cakehp-约定,主键始终命名为idhttp://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

2) 正如您在Employee模型中有一个belongsTo一样,您也应该在用户模型中添加一个hasOne关系-请参阅http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasone

3) 为了保存记录及其相关数据,您需要的方法是saveAll-查看此处的文档:http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall数组数据null数组选项数组