Model inheritance

Can anyone describe how to implement model inheritance, and how to use same Gii features generating model classes and CRUD generators as usual

for example if I have a table










id, // Same 1:1 relationship




Can someone explain how to generate models for these tables (including validations) and use Crud generators properly to save, and access product_auto objects?

I have tried several threads discussing this problem but not sure which one to use and some of them not working just giving errors


in both of it, create a model ( better use Gii) then add a relation, something like

//for the product one

public function relations(){

	return array(




//for the auto one

public function relations(){

	return array(




and then use it as following



more about relations can be found here

Thanks for the quick reply, I tried this way first, but when I use crud generator for ProductAuto and in the _form.php if I need to add the fields for product (base table) i’m getting errors, say I generate the crud files for productAuto and in the _form.php I need to get the data for the product

So I would write like follow

    <!-- getting data for product (base) tble -->

<div class="row">

	<?php echo $form->labelEx($model->product,'description'); ?>

	<?php echo $form->textField($model->product,'description'); ?>

	<?php echo $form->error($model->product,'description'); ?>


    <!-- getting data for product_auto (child) -->

<div class="row">

	<?php echo $form->labelEx($model,'color'); ?>

	<?php echo $form->textField($model,'color'); ?>

	<?php echo $form->error($model,'color'); ?>


But this doesn’t work giving error as follows

Create ProductAutomobile

Fields with * are required.

( ! ) Fatal error: Call to a member function isAttributeRequired() on a non-object in C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php on line 1163

Call Stack

Time Memory Function Location

1 0.0006 335712 {main}( ) …\index.php:0

2 0.0178 1819592 CApplication->run( ) …\index.php:13

you should send to the view the 2 models, both loaded with the same id in case of update, something like the following:

//in the controller




//in the index view


and thne in the form like you posted with a couple changes

<div class="row">

		<?php echo $form->labelEx($model_product,'description'); ?>

		<?php echo $form->textField($model_product,'description'); ?>

		<?php echo $form->error($model_product,'description'); ?>


        <!-- getting data for product_auto (child) -->

	<div class="row">

		<?php echo $form->labelEx($model_auto,'color'); ?>

		<?php echo $form->textField($model_auto,'color'); ?>

		<?php echo $form->error($model_auto,'color'); ?>


Thanks for the quick reply and I was able to save new product_auto with the product information with the help of your guidance, but there’s a small issue now.

I have marked product.description field required? so it is highlighted with the correctly with the * symbol in the data form. however if we just submit the form without providing data, it doesn’t return back requesting for it and just goes to the database insert and fails there?

in my product model it is marked as required under rules(), how do I fix this issue

Thanks again for your kind help

it is because you must validate both tables, check out this link

And no problem, im glad to help

Thanks a lot, it works, thank you very much for your help. Now I can proceed :) :)