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
product
{
id,
description,
make,
mode
}
product_auto
{
id, // Same product.id 1:1 relationship
color,
registration_number
}
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(
'auto'=>array(self::HAS_ONE,'ProductAuto','id'),
);
}
//for the auto one
public function relations(){
return array(
'product'=>array(self::BELONGS_TO,'Product','id'),
);
}
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
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
$model_auto=productAuto::model();
$model_product=product::model();
$this->render('index',array('model_auto'=>$model_auto,'model_product'=>$model_product));
//in the index view
$this->render('_form',array('model_auto'=>$model_auto,'model_product'=>$model_product));
and thne in the form like you posted with a couple changes
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