【问题标题】:Cakephp joining two models on insertCakephp 在插入时加入两个模型
【发布时间】:2013-08-16 09:08:28
【问题描述】:

好的,所以我有两张桌子 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. user_id 始终为 0,但在用户表中,user_id 为例如 21。

谁能告诉我为什么会这样以及我该如何解决?

更新

我的员工控制器中的添加操作现在如下所示:

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

还是没有变化

【问题讨论】:

    标签: php cakephp cakephp-model


    【解决方案1】:

    一些问题...

    1) 您的员工表的主键应该是 id,而不是employee_id。根据 cakephp 约定,主键始终命名为 id - http://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-array-data-null-array-options-array

    【讨论】:

    • 如果不是所有用户都有一名员工怎么办?
    • 那么您必须将关联更改为 User hasMany Employee
    • @burzum 但并非所有用户都有员工只有某些用户有员工其他用户与其他表有关系,例如客户
    • @MarcRasmussen 是的,但这没关系,因为 hasMany 的意思是“零或更多”。所以,零很好,一也很好。
    猜你喜欢
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多