CakePHP 2.4:通过模型别名使用HABTM assoc.和belongsTo assoc.设置模型,并随后进行saveAssociated()工作
当我试图建立这个模型时,我被我忽略(或根本误解!)的东西卡住了:CakePHP 2.4:通过模型别名使用HABTM assoc.和belongsTo assoc.设置模型,并随后进行saveAssociated()工作,cakephp,has-and-belongs-to-many,model-associations,Cakephp,Has And Belongs To Many,Model Associations,当我试图建立这个模型时,我被我忽略(或根本误解!)的东西卡住了: Accounts HABTM用户和管理员属于账户,其中管理员是用户的别名 我在RDBMS方面非常糟糕,因此进一步澄清: 帐户字段:id、姓名、管理员id等。 用户字段:id、name、dob等。 帐户用户字段:id、用户id、帐户id 至于模型关联,我有: 用户 public $hasOne = array( 'Account' => array( 'className' =&
Accounts HABTM用户
和管理员属于账户
,其中管理员
是用户
的别名
我在RDBMS方面非常糟糕,因此进一步澄清:
帐户字段:id、姓名、管理员id等。
用户字段:
id、name、dob等。
帐户用户字段:
id、用户id、帐户id
至于模型关联,我有:用户
public $hasOne = array(
'Account' => array(
'className' => 'Account',
'foreignKey' => 'administrator_id'
)
);
public $hasAndBelongsToMany = array(
'Account' =>
array(
'className' => 'Account',
'joinTable' => 'users_accounts',
'foreignKey' => 'user_id',
'associationForeignKey' => 'account_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'with' => ''
)
);
public $belongsTo = array(
'Administrator' => array(
'className' => 'User',
'foreignKey' => 'id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'users_accounts',
'foreignKey' => 'account_id',
'associationForeignKey' => 'user_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
客户
public $hasOne = array(
'Account' => array(
'className' => 'Account',
'foreignKey' => 'administrator_id'
)
);
public $hasAndBelongsToMany = array(
'Account' =>
array(
'className' => 'Account',
'joinTable' => 'users_accounts',
'foreignKey' => 'user_id',
'associationForeignKey' => 'account_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'with' => ''
)
);
public $belongsTo = array(
'Administrator' => array(
'className' => 'User',
'foreignKey' => 'id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'users_accounts',
'foreignKey' => 'account_id',
'associationForeignKey' => 'user_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
然而,我的账户
控制器经过烘焙后生成如下代码:
$this->Administrator->find('list')代码>
但当然没有管理员控制器,所以我只剩下:
错误:对非对象调用成员函数find()
文件:path/to/app/Controller/accountscocontroller.php
我已独立设置ACL;'在这种情况下,“管理员”在授权方面并不代表一类用户,只是关联而已,它可能更像“所有者”。我没有理由为他们创建一个单独的模型,我只是想区分创建帐户的用户和将使用它的其他用户(当然,创建者几乎肯定也会使用它)。如果这不会引起问题,我很乐意在数据库中将字段保留为Accounts
user\u id
,但(对我来说)它似乎注定会出现问题
另外一个问题是,我似乎无法使用我的userscoontroller
$this->User->saveAssociated($this->request->data)
这是我的视图文件:
<?php echo $this->Form->create('User', array('action' => "register/$token_id/$group_id/$account_id")); ?>
<fieldset>
<legend><?php echo __('Registration'); ?></legend>
<?php
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('firstname');
echo $this->Form->input('lastname');
echo $this->Form->input('claimed', array('type'=>'hidden', 'value'=>1));
echo $this->Form->input('studentno');
echo $this->Form->input('UsersAccount.account_id', array('type'=>'hidden', 'value'=>$account_id));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
我确信我的错误主要是理解了这些关系(即,这里有一个逻辑问题,而不是语法问题)。我已经广泛地阅读了《蛋糕手册》,但我不知道蛋糕是如何做到这一点的,我宁愿学习正确的方法,也不愿构建绕过蛋糕优雅的技巧。因此,非常感谢您的帮助。:)
- 在您的Accounts controller中,如果希望显式删除$uses数组,则必须通过Accounts模型来提取管理员模型
$this->Account->Administrator->find('list')
- 您必须在saveAssociated参数中将deep key设置为true,它才能处理关联
$this->User->saveAssociated($this->request->data,array('deep'=>true))
注意:保存除HABTM以外的所有协会的相关作品Beauty,谢谢您;别名现在可以工作了,我将停止尝试使用saveAssociated()保存HABTM关系。那么,想必,为了保存HABTM关系,我确实只是从控制器操作中的请求数据构建它们,并创建()保存()它们?使用HABTM来保存模型,因为该模型中的关联将属于正确的?