2015-04-05 92 views
0

当我保存多个模型时,我无法将Company.id转换为License.company_id。我需要company_id许可证表的原因是公司拥有许可证并将许可证分配给其用户/员工。saveA关联3个模型

我有这个表:公司,用户和许可证。

许可证:belongsTo公司,hasMany用户。

公司:hasMany许可证,hasMany用户。

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

编辑:我曾尝试相同的代码只是另一种模式(公司)为好。 User.license_id保存正确,但是Lisence.company_id不会。

这是我之前$这个 - 获取>用户创建():

Array 
(
    [Company] => Array 
     (
      [name] => Company AS 
     ) 

    [User] => Array 
     (
      [name] => Ola Nordmann 
      [email] => [email protected] 
      [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 
     ) 

) 
+0

你可以做'PR($ this-> request-data);'在'$ this-> User-create()' – 2015-04-05 20:47:23

+0

之前,我用输出更新了第一篇文章。这是我想的(想要的)。现在查询中缺少的是[License] [company_id]。 – Christian 2015-04-05 21:46:51

回答

0

目前,它节省了用户与它的关联公司,并与它用户的相关许可。您的代码中没有任何内容表明许可证和公司之间应该存在直接关联。

公司可以有许多许可证。在这种格式的数据中,CakePHP如何知道您打算将此特定用户许可也归于其公司?

这就是说,你可能需要将其分解成不同的保存。

关闭我的头顶,我可能会保存用户只是它的数据,然后保存公司与它的相关许可证。由于此时您已具有user_id,因此您可以在要保存的数据中包含预填充的Company.user_idLicense.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 
0

现在我必须做这件事,但我不知道是否有其他更容易OG更好的方法来做到这一点。

这是我的代码:https://gist.github.com/sjundee/2683820962ab24047bb8

在寄存器()函数我第一次在公司,用户和许可证保存新的记录,但License.company_id不保存,我$this->User->Company->id更新License.company_id这让我的行ID我保存到公司。

+0

这与我提供的解释有什么不同,解释了为什么它不起作用,并说你需要“将此分解成不同的保存”? – Dave 2015-04-06 15:08:30

+0

是的,谢谢你的帮助。答案是对你评论的回复。我仍然认为他们是一种更简单的方法,但不知道如何。 – Christian 2015-04-06 16:38:20