CakePHP multi-model view
Asked Answered
T

3

5

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.

Tildie answered 21/9, 2008 at 4:19 Comment(0)
L
10

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

Lanielanier answered 22/9, 2008 at 0:44 Comment(0)
D
4

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.

Deccan answered 21/9, 2008 at 6:59 Comment(2)
Hi. I changed my question to reflect my intention, how to save, how I create the Registration Controller and view(s)Tildie
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.Deccan
D
1

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'
));
Derr answered 25/9, 2008 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.