0

我有两张桌子UsersClients

用户表敲击如下所示:

  id (primary)
  username(varchar)
password, client_id

现在我的客户表如下所示:

user_id, client_id(primary), is_new

它们之间的关系定义如下:

    class User extends AppModel
{
    public $name = 'User';
    public $hasOne = array(
        'Client' =>array(
            'className' => 'Client',
            'dependent' => true,
            'foreignKey' => 'client_id'
        )
    );
}


    class Client extends AppModel
{
    public $name = 'Client';
    public $primaryKey = 'client_id';
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'dependent' => true,
            'foreignKey' => 'user_id'
        )
    );
}

现在在我的用户控制器中,我有以下保存操作:

      if ($this->request->is('post')) {
        $data = null;
        $client_id = null;
        if ($this->request->data['User']['group_id'] == 1) {
            $client_id = 2;

        } elseif ($this->request->data['User']['group_id'] == 2) {
            $client_id = $this->request->data['User']['Client id'];
        }
        $data = array('User' => array(
            'username' => $this->request->data['User']['username'],
            'password' => $this->request->data['User']['password'],
            'group_id' => $this->request->data['User']['group_id'],
            'client_id' => $client_id),
        'Client' => array('client_id' => $client_id)
        );

        if ($this->User->save($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.'));
        }
    }
    $groups = $this->User->Group->find('list');
    $this->set(compact('groups'));
}

当我保存一个新实体时,将使用正确的 client_id 添加到 users 表中,但没有将任何内容添加到 clients 表中。

谁能告诉我为什么会这样?

请注意,我已尝试将功能更改为saveAll();

更新

如果我执行以下操作:

        $data = array( 'User' => array( 'username' => $this->request->data['User']['username'],
                'password' => $this->request->data['User']['password'],
                'group_id' => $this->request->data['User']['group_id'],
                'client_id' => $client_id ),
                'Client' => array('client_id' => $client_id) );
            if ($this->User->Client->saveAll($data)) {
}

又名使用$this->User->Client->saveAll而不是$this->User->saveAll()

一切正常..

现在这很好,但它应该是那样的吗?

4

2 回答 2

1

使用$this->User->saveAssociated($data)代替$this->User->save($data)

更新

只需为没有字段id的客户端设置数据,如下所示,以便在保存用户的同时保存数据。

$data = array(
    'User' => array(
        'username' => $this->request->data['User']['username'], 
        'password' => $this->request->data['User']['password']
    ), 
    'Client' => array(
        'field1' => 'field1value',
        'field2' => 'field2value',
        'field3' => 'field3value'
    )
);
于 2013-09-19T09:30:40.693 回答
1

是的,它们都是相同的,它们之间的唯一区别是,当您需要一次保存关联模型时使用 saveAssoicated,而 saveAll 是 saveAssociated 的包装器,

您可以使用saveAssociated

$this->User->saveAssociated($data)

以及saveAll()

$this->User->saveAll($data)

谢谢

于 2013-09-19T10:06:03.270 回答