Php SaveAssociated有3个型号

Php SaveAssociated有3个型号,php,mysql,cakephp,cakephp-2.0,Php,Mysql,Cakephp,Cakephp 2.0,当我保存多个模型时,我无法将Company.id转换为License.Company\u id。我在License表中需要Company\u id的原因是该公司拥有该许可证,并将为其用户/员工分配一个许可证 我有以下表格:公司、用户和许可证 许可证:属于公司,有许多用户 公司:有很多许可证,有很多用户 用户:belongsTo公司,belongsTo许可证 公司表: id name 用户表: id company_id license_id email password 许可证表: id c

当我保存多个模型时,我无法将Company.id转换为License.Company\u id。我在License表中需要Company\u id的原因是该公司拥有该许可证,并将为其用户/员工分配一个许可证

我有以下表格:公司、用户和许可证

许可证:属于公司,有许多用户

公司:有很多许可证,有很多用户

用户:belongsTo公司,belongsTo许可证

公司表:

id
name
用户表:

id
company_id
license_id
email
password
许可证表:

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'));
        }
    }
}
用户/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')); ?>

目前,它正在保存一个与其关联的公司的用户,以及一个与其关联的许可证的用户。代码中没有任何内容表明许可证和公司之间存在直接关联

一家公司可以有许多许可证。在这种数据格式中,CakePHP如何知道您打算让这个特定用户的许可证也属于他们的公司

话虽如此,您可能需要将其分解为不同的保存

在我脑海中,我可能只使用用户的数据保存用户,然后使用相关的许可证保存公司。因为此时您已经有了用户id,所以可以在要保存的数据中包含预填充的
公司.user\u id
许可证.0.user\u 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

现在我有了一种方法,但我不知道是否还有其他更简单更好的方法

这是我的代码:


在register()函数中,我首先将新记录保存在公司、用户和许可证中,但作为
许可证。公司id
未保存,我使用
$this->User->company->id
更新
许可证。公司id
将获得保存到公司中的行id。

您能做一个
请购($this->request data)吗
就在
$this->User-create()之前,我用输出更新了第一篇文章。我想这是我所期望的。现在,查询中缺少的是[License][company_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