cakePHP保存条目以连接现有记录表

时间:2013-03-07 13:48:41

标签: cakephp model has-and-belongs-to-many jointable

首先简短说明。我必须建模:帐户和用户以及account_users的连接表。模型在每个模型上都有一个habtm关联:

用户模型:

    'Account' => array(
        'className' => 'Account',
        'joinTable' => 'accounts_users',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'account_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )

帐户模型:

        'User' => array(
        'className' => 'User',
        'joinTable' => 'accounts_users',
        'foreignKey' => 'account_id',
        'associationForeignKey' => 'user_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )

现在我试图手动保存这两者之间的关系只是来自所有现有条目的连接表acccounts_users,这里是我的代码

        $account = base64_decode($this->params['pass']['0']);
    $token = $this->params['pass']['1'];


    if($user = $this->User->findByToken($token))
    {
        // ZUr test zwecken
       # $this->User->query(" INSERT INTO accounts_users (account_id ,user_id) VALUES (222, 223); ");
        $aId = $this->Account->findById($account);
        $this->data['User'][0]['user_id'] = $user['User']['id'];
        $this->data['Account'][0]['account_id'] = $aId['Account']['id'];



        $this->User->bindModel(array(
            'hasMany' => array('AccountsUser')
        ));
        $this->User->saveAll($this->data, array('validate' => false));


        print_r('gefunden'); die;
        $this->Redirect->flashSuccess('Account Invitation successfull. Log In!', array('controller' => 'users', 'action' => 'login'));
    }
    else
    {
        print_r('nicht gefunden'); die;
        // user nicht gefunden zum login umleiten
        $this->Redirect->flashWarning('Account Invitation error. Please try again!', array('controller' => 'users', 'action' => 'login'));

    }

结果是accounts_users表上的新条目,但user_id为0.我不明白为什么缺少用户ID,因为它通过了corectly。即使我在数据数组中传递一些ID,也只是在没有用户ID的情况下写入account_id。

更新

我玩了一些模型,并通过帐户模型将数据保存到accounts_users,查看更新后的代码:

            $this->data['User']['id'] = $user['User']['id'];
        $this->data['Account']['id'] = 33; #$aId['Account']['id'];

        $this->Account->AccountsUser->create();

        $this->Account->saveAll($this->data, array('validate' => false));

所以现在脚本会插入两个ID,但是如果有另一个用户具有相同帐户ID的条目,则用户会被覆盖。其他任何工作。有关如何为新用户创建具有分组帐户ID的新条目的任何想法吗?

这是我得到的mysql查询:

更新accounts设置id = 33 WHERE accountsid = 33

SELECT AccountsUseruser_id FROM accounts_users AS AccountsUser WHERE AccountsUseraccount_id = 33

删除AccountsUser FROM accounts_users AS AccountsUser WHERE AccountsUseraccount_id = 33 AND

INSERT INTO accounts_usersaccount_iduser_id)价值观(33,'32')

任何想法为什么?提前致谢

1 个答案:

答案 0 :(得分:1)

CakePHP 1.x将HABTM联接表视为“愚蠢”;它将删除所有现有记录并插入新记录以替换它们。如果您的连接表还包含其他数据,那么这是一个主要的PITA。 (可以通过在beforeSave()回调中添加一些代码来防止这种情况发生)

CakePHP 2.1为HABTM关系提供了选项keepExisting。此选项可防止CakePHP删除JOIN表中的记录。如果这是一个新项目我真的建议使用CakePHP 2.x作为批次自CakePHP 1.x以来有所改进。

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model

可以在此处找到有关在连接表中保存数据的一些提示;

http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-hasmany-through-data

对于CakePHP 1.3(见下面'当HABTM变得复杂时)

http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html#saving-related-model-data-habtm