Question

I am very new to cake and I was wondering how i would be able to create a new user and also be able to create the contact details

database is like so

Users

  • user_id
  • contact_detail_id
  • password
  • role_id
  • username

Contact_Details

  • contact_detail_id
  • address1
  • address2

and many other fields

In the User model I have assigned public $hasOne = array('ContactDetail');

Since the user will have one contactdetail

Now what I need to know is how would this would be saved in the add method as many said to use the saveassociated although this doesn't seem to be working.

The add view is like so and from my understanding this is correct

<?php echo $this->Form->create('User', array('action' => 'add')); ?>
    <fieldset>
        <legend><?php echo __('Add User'); ?></legend>
    <?php
        echo $this->Form->input('User.username');
                echo $this->Form->input('User.password');
                echo $this->Form->input('roles');

                echo $this->Form->input('ContactDetail.name');
                echo $this->Form->input('ContactDetail.surname');
                echo $this->Form->input('ContactDetail.address1');
                echo $this->Form->input('ContactDetail.address2');
                echo $this->Form->input('ContactDetail.country');
                echo $this->Form->input('ContactDetail.email');
                echo $this->Form->input('ContactDetail.fax');


          ?>      
                <label>Are you interested in buying property in Malta?</label>
          <?php  
                $interest_buy = array('0'=>'no','1' => 'yes');
                echo $this->Form->input('ContactDetail.interest_buy_property',array('type'=>'radio','options'=>$interest_buy,'value'=>'0','legend'=>FALSE));
          ?>      
                <label>Are you interested in renting property in Malta?</label>
          <?php  
                $interest_rent = array('0'=>'no','1' => 'yes');
                echo $this->Form->input('ContactDetail.interest_rent_property',array('type'=>'radio','options'=>$interest_rent,'value'=>'0','legend'=>FALSE));
                echo $this->Form->input('ContactDetail.mobile');
                echo $this->Form->input('ContactDetail.phone');
                echo $this->Form->input('ContactDetail.postcode');
                echo $this->Form->input('ContactDetail.town');

                echo $this->Form->input('ContactDetail.newsletter',array('type'=>'checkbox','label'=>'Would you like to register for the newsletter?' ,'checked'=>'1','legend'=>FALSE,));
         ?>       

    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
Was it helpful?

Solution

Please see Arun explanation about what to put in your model first and then, you can use $this->User->saveAll(); or $this->User->saveAssociated(); to save both tables at the same time.

If you want to save ContactDetail first and then save User, You can do this second option, but it is not recomended.

 $this->User->ContactDetail->create(); 
 $this->User->ContactDetail->save($this->request->data);`
 this->request->data['User']['contact_detail_id'] = $this->User->ContactDetail->id`;
 $this->User->Create();
 $this->User->save($this->request->data);`

But I prefer the first option, to try the first option again,

Note: Make sure ContactDetail and User relationship are there in the model like this in User Model

public $hasOne= array(
    'ContactDetail'   => array(
        'className' => 'ContactDetail',         
        'foreignKey' => 'contact_detail_id'
    )
);

Hope it helps.

OTHER TIPS

In the first look into your database, you should use the following database structure, this will help you in a more directions:

User Model

id
username
password
role_id

ContactDetail Model

id
user_id
address1
address2

Define hasMany model relationship in the User Model with ContactDetail like:

$hasMany => array('ContactDetail');

Now populate an array of Contact details with (id => address1) pair using:

$this->ContactDetail->find('list', array('conditions' => array('user_id' => $user_id)),
                                         'fields' => array('ContactDetail.id', 'ContactDetail.address1')));

Now to save an associated details, use SaveAssociated Method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top