tags:

views:

40

answers:

1

I have following situation:

I'm developing an address-application to store the details of our clients.

I've got the following db-structure:

Clients hasMany Addresses
Addresses belongsTo Client
Addresses hasMany AddressEmails and AddressPhones

If someone now adds a new client, his primary address with 1 email and 1 phonenumber should be saved.

That works partly very good if I do this in the add-view of the client with the following code and call the saveAll-method in the client-controller:

 <?php
  echo $this->Form->input('client_group_id');
  echo $this->Form->input('company');
  echo $this->Form->input('firstname');
  echo $this->Form->input('lastname');
  echo $this->Form->input('www');
  echo $this->Form->input('Address.0.is_standard', array('type' => 'hidden', 'value' => '1'));
  echo $this->Form->input('Address.0.street');
  echo $this->Form->input('Address.0.zip');
  echo $this->Form->input('Address.0.city');
 ?>

But I have no idea how to save the email and the phone number over this view. Tried thinks like AddressEmail.0.email but that didn't work.


Got it:

With Nik's posted solution it's working fine, I'm now calling $this->Client->Address->saveAll($this->data) but had to adjust my add-view like this:

<?php
    echo $this->Form->input('client_group_id');
    echo $this->Form->input('company');
    echo $this->Form->input('firstname');
    echo $this->Form->input('lastname');
    echo $this->Form->input('www');
    echo $this->Form->input('Address.is_standard', array('type' => 'hidden', 'value' => '1'));
    echo $this->Form->input('Address.street');
    echo $this->Form->input('Address.zip');
    echo $this->Form->input('Address.city');
    echo $this->Form->input('AddressEmail.0.type', array('type' => 'hidden', 'value' => 'Standard'));
    echo $this->Form->input('AddressEmail.0.email');
    echo $this->Form->input('AddressPhone.0.type', array('type' => 'hidden', 'value' => 'Standard'));
    echo $this->Form->input('AddressPhone.0.number');
?>

Since I'm now calling the saveAll()-method from the Address-Model the zeros are not needed anymore!

+1  A: 

Saving related data with saveAll() will only work for directly associated models.

So basically you could save with saveAll() Client (as primary model) and Addresses. Emails and Phones you could do by saving from the Adress model like:

$this->Client->saveAll($data); //This saves Client and Address
$this->Client->Address->saveAll($data); //This saves Email and Phone

I haven't test it but it should work this way. (assuming that your fields follow the naming convention like:

echo $this->Form->input('Email.0.field');
echo $this->Form->input('Email.1.field');
echo $this->Form->input('Email.2.field');
echo $this->Form->input('Phone.0.number');
echo $this->Form->input('Phone.1.number');
echo $this->Form->input('Phone.2.number');

I am not sure if it will "catch" the Address ID directly, but you could try.

Another way is to save model from

$this->Client-Address->saveAll($data);

only. This way all related models will be saved (according to documentation - see first quite).

Nik
Hi Nik,thank you, that's a good point!Unfortunately, if I use the first version it saves the client two times but the relations are correct.If I just use the 2nd version, everythings okay, but it won't save the address-details. The record is created but doesn't get saved.
Tim