Saving multiple instances of the same model

Hi all,

I’ll illustrate my need with an example.

There are 2 tables (2 models)


Clients:

---id

---name


Addresses:

---id

---address

---client_id

It’s a one-to-many relationship. 1 Client can have multiple Addresses.

How would I go about saving a new client and multiple addresses from one form?

Form:

Client Name:[______]

[Add address button] <- when clicked new address fields pop out

Address: [______]

user clicks again

Address: [______]

I got one to one relationships working, but this got me confused.

My one to one Controller looks something like this:


$model = new Clients();

$model->load(Yii::$app->request->post()) && $model->save();


$model2 = new Addresses();

$model2->load(Yii::$app->request->post());

$model2->setAttribute('client_id', $model->id);

$model2->save();

Thank you :)