views:

2456

answers:

3

Heyall,

I am creating a website in CakePHP and I am kind of new on it. I couldn't find good resources on this matter, so there you go:

I have a three table structure for registering users: Users, Addresses and Contacts. I have to build a view with info of all three tables like:

Full Name:         [          ] (from Users)
Shipping Address:  [          ] (from Address)
Mobile Phone:      [          ] (from Contact)
e-Mail Address:    [          ] (from Contact)

What is the best way to deal with this situation. Specially for saving. Creating a new Model to represent this, that will have a save() method itself (Maybe a sql view in the database) Create a Controller to deal with this View that binds or unbinds info

I wonder still how I will handle both contacts as they will be 2 different INSERT's

Any hint or resources I can dig of I will be glad.

+4  A: 

CakePHP allows you to easily maintains link between your models using relationship, see http://book.cakephp.org/view/78/Associations-Linking-Models-Together for the complete detail. Then retreiving the right User, you'll also get "for free", its address and contact informations.

gizmo
Hi. I changed my question to reflect my intention, how to save, how I create the Registration Controller and view(s)
Fernando Barrocal
CakePHP manage itself saving composed objects. Is means that if you load a User and change its contact, when you call the implicit "save" mothed from CakePHP on the User object, the Contact object will be updated as well.
gizmo
+9  A: 

If your using the latest 1.2 code, check out Model::saveAll in the api

eg. Your view might look something like this:

echo $form->create('User', array('action' => 'add');
echo $form->input('User.name');
echo $form->input('Address.line_1');
echo $form->input('Contact.tel');
echo $form->end('Save');

Then in your Users controller add method you'd have something like:

...

if($this->User->saveAll($this->data)) {
 $this->Session->setFlash('Save Successful');
 $this->redirect(array('action' => 'index'));
} else {
 $this->Session->setFlash('Please review the form for errors');
}

...

In your User model you will need something like:

var $hasOne = array('Address', 'Contact');

Hope that helps!

http://api.cakephp.org/class_model.html#49f295217028004b5a723caf086a86b1

+1  A: 

3 models : User, Address, Contact

User hasOne Address, Contact
Address belongsTo User
Contact belongsTo User

in your model you define this like this :

class User extends AppModel {
var $name = 'User';
var $hasOne = array('Address','Contact');
..

To make this view, you need user_id field ind addresses, and contacts tables

To use this in a view, you simply call a find on the User model with a recursive of one (and btw, the users controller only uses User model).

$this->User->recursive = 1;
$this->set('user', $this->User->find('first', array('conditions'=>array('id'=>666)));

This will result in this array for your view :

array(
  'Use' => array(
     'id' => 666,
     'name' => 'Alexander'
),
  'Address' => array(
     'id' => 123,
     'zip' => 555
),
   'Contact' => array(
     'id' => 432,
     'phone' => '555-1515'
));
Alexander Morland