save与3个模型相关联
saveAssosiated with 3 models
当我保存多个模型时,我无法将 Company.id 放入 License.company_id。我需要 company_id 许可证 table 的原因是该公司拥有该许可证,并将向其用户/员工分配许可证。
我有这个 table:公司、用户和许可证。
许可证:属于公司,有许多用户。
公司:有很多许可证,有很多用户。
用户:属于公司,属于许可证。
公司table:
id
name
用户table:
id
company_id
license_id
email
password
许可证 table:
id
company_id
UsersController.php
public function register() {
$expires = date("Y-m-d H:i:s", strtotime("+30 days"));
$this->set('expires', $expires);
if ($this->request->is('post')) {
$this->request->data['User']['language'] = 'nor';
$this->request->data['User']['role'] = 'admin';
$this->request->data['User']['active'] = '1';
$this->User->create();
if ($this->User->saveAssociated($this->request->data, array('deep' => true))) {
$this->Session->setFlash(__('The user has been saved.'), 'default', array('class' => 'notice success'));
return;// $this->redirect(array('controller' => 'landing', 'action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'default', array('class' => 'notice error'));
}
}
}
Users/register.ctp
<?php echo $this->Form->create('User', array('controller' => 'users', 'action' => 'register')); ?>
<?php
echo $this->Form->input('Company.name', array('placeholder' => __('Company')));
echo $this->Form->input('name', array('placeholder' => __('Name')));
echo $this->Form->input('email', array('class' => 'form-control', 'placeholder' => __('E-mail')));
echo $this->Form->input('mobile', array('placeholder' => __('Mobile number')));
echo $this->Form->input('password', array('placeholder' => __('Password')));
echo $this->Form->input('License.product_id', array('type' => 'hidden', 'value' => '1'));
echo $this->Form->input('License.amount', array('type' => 'hidden', 'value' => '2'));
echo $this->Form->input('License.renewal', array('type' => 'hidden', 'value' => '0'));
echo $this->Form->input('License.expires', array('type' => 'hidden', 'value' => $expires));
?><?php echo $this->Form->end(__('Submit')); ?>
编辑:我也在另一个模型(公司)中尝试过相同的代码。 User.license_id 保存正确,但 Lisence.company_id 不正确。
这是我在 $this->User-create():
之前得到的
Array
(
[Company] => Array
(
[name] => Company AS
)
[User] => Array
(
[name] => Ola Nordmann
[email] => ola@nordmann.no
[mobile] => 99229922
[password] => qwertyui
[language] => nor
[role] => admin
[active] => 1
)
[License] => Array
(
[product_id] => 1
[amount] => 2
[renewal] => 0
[expires] => 2015-05-05 23:39:10
)
)
目前,它正在保存一个用户及其关联公司,以及一个用户及其关联许可。您的代码中没有任何内容表明许可证和公司之间应该有直接关联。
一家公司可以有很多许可证。在这种格式的数据中,CakePHP 如何知道您打算将此特定用户许可也属于他们的公司?
话虽如此,您可能需要将其分成不同的存档。
在我的脑海中,我可能会只保存用户的数据,然后保存公司及其相关的许可证。因为此时您已经有了 user_id,所以您可以在要保存的数据中包含预填充的 Company.user_id
和 License.0.user_id
字段。
// pseudo code:
// Save User via the User model
// get User's new id based on the just-created row
// Create data array from data passed by user and just-retrieved user_id
// save Company via the Company model including it's associated License
现在我有一种方法可以做到,但我不知道是否有其他更简单或更好的方法可以做到。
这是我的代码:https://gist.github.com/sjundee/2683820962ab24047bb8。
在 register() 函数中,我首先在公司、用户和许可证中保存新记录,但由于未保存 License.company_id
,我将 License.company_id
更新为 $this->User->Company->id
这让我我保存到 Companies 中的行 ID。
当我保存多个模型时,我无法将 Company.id 放入 License.company_id。我需要 company_id 许可证 table 的原因是该公司拥有该许可证,并将向其用户/员工分配许可证。
我有这个 table:公司、用户和许可证。
许可证:属于公司,有许多用户。
公司:有很多许可证,有很多用户。
用户:属于公司,属于许可证。
公司table:
id
name
用户table:
id
company_id
license_id
email
password
许可证 table:
id
company_id
UsersController.php
public function register() {
$expires = date("Y-m-d H:i:s", strtotime("+30 days"));
$this->set('expires', $expires);
if ($this->request->is('post')) {
$this->request->data['User']['language'] = 'nor';
$this->request->data['User']['role'] = 'admin';
$this->request->data['User']['active'] = '1';
$this->User->create();
if ($this->User->saveAssociated($this->request->data, array('deep' => true))) {
$this->Session->setFlash(__('The user has been saved.'), 'default', array('class' => 'notice success'));
return;// $this->redirect(array('controller' => 'landing', 'action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'default', array('class' => 'notice error'));
}
}
}
Users/register.ctp
<?php echo $this->Form->create('User', array('controller' => 'users', 'action' => 'register')); ?>
<?php
echo $this->Form->input('Company.name', array('placeholder' => __('Company')));
echo $this->Form->input('name', array('placeholder' => __('Name')));
echo $this->Form->input('email', array('class' => 'form-control', 'placeholder' => __('E-mail')));
echo $this->Form->input('mobile', array('placeholder' => __('Mobile number')));
echo $this->Form->input('password', array('placeholder' => __('Password')));
echo $this->Form->input('License.product_id', array('type' => 'hidden', 'value' => '1'));
echo $this->Form->input('License.amount', array('type' => 'hidden', 'value' => '2'));
echo $this->Form->input('License.renewal', array('type' => 'hidden', 'value' => '0'));
echo $this->Form->input('License.expires', array('type' => 'hidden', 'value' => $expires));
?><?php echo $this->Form->end(__('Submit')); ?>
编辑:我也在另一个模型(公司)中尝试过相同的代码。 User.license_id 保存正确,但 Lisence.company_id 不正确。
这是我在 $this->User-create():
之前得到的 Array
(
[Company] => Array
(
[name] => Company AS
)
[User] => Array
(
[name] => Ola Nordmann
[email] => ola@nordmann.no
[mobile] => 99229922
[password] => qwertyui
[language] => nor
[role] => admin
[active] => 1
)
[License] => Array
(
[product_id] => 1
[amount] => 2
[renewal] => 0
[expires] => 2015-05-05 23:39:10
)
)
目前,它正在保存一个用户及其关联公司,以及一个用户及其关联许可。您的代码中没有任何内容表明许可证和公司之间应该有直接关联。
一家公司可以有很多许可证。在这种格式的数据中,CakePHP 如何知道您打算将此特定用户许可也属于他们的公司?
话虽如此,您可能需要将其分成不同的存档。
在我的脑海中,我可能会只保存用户的数据,然后保存公司及其相关的许可证。因为此时您已经有了 user_id,所以您可以在要保存的数据中包含预填充的 Company.user_id
和 License.0.user_id
字段。
// pseudo code:
// Save User via the User model
// get User's new id based on the just-created row
// Create data array from data passed by user and just-retrieved user_id
// save Company via the Company model including it's associated License
现在我有一种方法可以做到,但我不知道是否有其他更简单或更好的方法可以做到。
这是我的代码:https://gist.github.com/sjundee/2683820962ab24047bb8。
在 register() 函数中,我首先在公司、用户和许可证中保存新记录,但由于未保存 License.company_id
,我将 License.company_id
更新为 $this->User->Company->id
这让我我保存到 Companies 中的行 ID。