Marc Rasmussen
Marc Rasmussen

Reputation: 20555

Cakephp saving data to additonal model

I have a User model that has a belongsTo relation ship with Profile

Now whenever I want to create a user I also want to create my profile.

For this I have created the following form:

 <div class="span9" id="lejer">
    <?php echo $this->Form->create('User', array('class' => 'form-horizontal')); ?>
    <?php echo $this->Form->input('group_id', array('type' => 'hidden', 'value' => 3));?>
    <h3 class="heading3">Dine Personlige Informationer:</h3>

    <div class="registerbox">
        <fieldset>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Fornavn:
                </label>

                <div class="controls">
                    <?php echo$this->Form->input('Fornavn', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Efternavn:
                </label>

                <div class="controls">
                    <?php echo$this->Form->input('Efternavn', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Telefon:
                </label>

                <div class="controls">
                    <?php echo$this->Form->input('Telefon', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    E-mail:
                </label>
                <div class="controls">
                    <?php echo$this->Form->input('E-mail', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>

        </fieldset>

    </div>

    <h3 class="heading3">Your Address</h3>
    <div class="registerbox">
        <fieldset>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Addresse:
                </label>
                <div class="controls">
                    <?php echo$this->Form->input('Addresse', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Postnr:
                </label>
                <div class="controls">
                    <?php echo$this->Form->input('Postnr', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    By:
                </label>
                <div class="controls">
                    <?php echo$this->Form->input('By', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Område:
                </label>
                <div class="controls">
                    <?php echo$this->Form->input('Område', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
        </fieldset>

    </div>

Now when I debug I can see that all of the data is within the following array:

$this->request->data['User'];

Now in my UsersController I have the following create action:

    public function create() {
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->saveAll($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

but when I run this only a User is created NOT a Profile.

Can anyone tell me what I am doing wrong? I thought that saveAll ment that I would save to its relation model.

Upvotes: 0

Views: 107

Answers (1)

Lakhwinder Singh
Lakhwinder Singh

Reputation: 5582

There is a problem in your code you need to user model name with field name with dot separator like

$this->Form->input('User.email'); $sthis->Form->input('Profile.Telefon');

After this you can your below code in your controller to save record.

$this->Model->saveAssociated($this->request->data);

for more detail please read this link:-

> http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array

Upvotes: 1

Related Questions