首先是一个简短的描述。我必须模型:帐户和用户以及它的连接表accounts_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,因为它正确地传递了。即使我在数据数组中手动传递了一些 id,它只写了 account_id 而没有用户 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 在哪里accounts
。id
= 33
选择AccountsUser
。user_id
从哪里来。accounts_users
_ AccountsUser
_ = 33AccountsUser
account_id
AccountsUser
从accounts_users
哪里删除。AccountsUser
_ = 33 和AccountsUser
account_id
插入accounts_users
( account_id
, user_id
) 值 (33,'32')
任何想法为什么?提前致谢