Crud Second Model In First Model's Update Form?

Let says I have two models like this:


driver(id, name)

car(reg, color, driver_id)

How can I do something like this: In the CRUD update form for one particular driver, I have AJAX CRUD form to add/edit/delete cars for this very same guy? Please, if possible, give your tips/advises with sample code as I am a real Yiibie :"> Thank you for your help!

Hi Ackley, you can easily create/add/edit models. In your drivers controller you could do something like this:

$carModel = new car;

$carModel->driver_id = $id;

$carModel->color = "red";

$carModel->reg = "example";

$carModel->save();

To edit or delete you’ll need to load the model or use relations instead, like:

$carModel = car::model()->findByPk($id);

$carModel->color = "blue";

$carModel->reg = "example 2";

$carModel->save();

or with relations:

$model->car->color = "blue";

There is a delete method just as a save method:

$carModel = car::model()->findByPk($id);

$carModel->delete();

You’ll probably be setting the model attributes from post from a form.

That’s a basic demonstration but it can get you started. Be sure to read about relations and check out the yii guide in general.

Hope that helps, happy coding!

Thank you clayton.caleb for your kindness! After a while researching, I found this excellent extension which do exactly what I need (I reckon this is a quite common need). Now I am diving into this to learn more :)

http://www.yiiframework.com/extension/multimodelform