Cameron
Cameron

Reputation: 28783

CakePHP form that talks to two tables and controllers

In my app I have a users controller and a profiles controller complete with tables and models for each. When a user signs up for an account they will create both a user account and a profile. So they will be saving data to two tables!

How do I do this though? As the logic is in two different models and controllers which is fine when they come to edit it as they are separate bits but when registering it becomes cloudy as to how this would work :/

Can anyone help? Thanks

Upvotes: 0

Views: 124

Answers (3)

AlexBrand
AlexBrand

Reputation: 12401

It is important to have relations between models. You don't say if you have them or not. If not, you should define relations such as:

User hasOne Profile
Profile belongsTo User

This will allow you to access one model from the other, and viceversa. As recommended by boobyWomack, you should check out this link which gives examples of the saveAll method.

Cheers

Upvotes: 0

Luke Barker
Luke Barker

Reputation: 915

you can use saveAll () instead of save(). This will let you save a model (eg User) along with its related Model (Profile) .

A nice easy description here in the offical docs: http://book.cakephp.org/view/1032/Saving-Related-Model-Data-hasOne-hasMany-belongsTo

also this blog post is old but worth reading so you understand. http://nuts-and-bolts-of-cakephp.com/2008/08/01/practical-use-of-saveall-part-1-working-with-multiple-models/

Upvotes: 1

Naftali
Naftali

Reputation: 146302

$this->ModelName1->save($this->data);
$this->ModelName2->save($this->data);

As long as the data variable is created correctly, the model should save it correctly as well ^_^

Upvotes: 0

Related Questions