0
votes

When I'm saving multiple models I can't get Company.id into License.company_id. The reason why I need company_id in license table is that the company owns the license and will assign a license to their users / employees.

I have this tables: Companies, Users and Licenses.

License: belongsTo Company, hasMany User.

Company: hasMany License, hasMany User.

User: belongsTo Company, belongsTo License.

Companies table:

id
name

Users table:

id
company_id
license_id
email
password

License 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')); ?>

Edit: I have tried the same code just in another model (Company) as well. User.license_id save correctly, but Lisence.company_id doesn't.

This is what I get before $this->User-create():

    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
        )

)
2
Could you do a pr($this->request-data); right before the $this->User-create()Kristoffer Darj
I updated the first post with the output. It is as expected (and wanted) I think. Now whats missing in the query is [License][company_id].Christian

2 Answers

0
votes

Currently, it's saving a User with it's associated Company, and a User with it's associated License. There is nothing in your code indicating that there should be a direct correlation between the License and Company.

A Company can have many Licenses. In this format of data, how would CakePHP know that you intend this particular User's License to also belongs to their Company?

That being said, you'll probably need to break this into different saves.

Off the top of my head, I would probably save the User with just it's data, then save the Company with it's associated License. Because you already have the user_id at that point, you can include the pre-populated Company.user_id and License.0.user_id fields in the data to save.

// 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
votes

Now I have one way to do it, but I don't know if there are other easier og better ways to do it.

This is my code: https://gist.github.com/sjundee/2683820962ab24047bb8.

In register() function I first save new records in Companies, Users and Licenses, but as License.company_id isn't saved, I update License.company_id with $this->User->Company->id which gets me the row-id I saved into Companies.