Take the 2-minute tour ×

I have four models in Yii. Car, RaceCar, LuxuryCar, and Driver.

RaceCar has_one Driver

LuxuryCar has_one Driver

Both RaceCar and LuxuryCar inherit from the same model - Car - using same table inheritance.

There is a "fuel_level" attribute in both LuxuryCar and RaceCar that changes the validation rules in Driver (Driver should not be able to set attribute "action" to "driving" if RaceCar.fuel_level is zero).

Rather than have to add every possible "belongs to" relation to Driver (I'm likely to add more inherited Car models), and then checking to see which kind of Car the Driver currently belongs to so I can load the model from the database again, I'd like to be able to validate the values of Driver's properties in the parent (e.g. RaceCar).

Is this possible? If so, how? If not, is there a better way that I'm missing?

share|improve this question
Looks like someone edited out the bits of the question that would have explained it to everyone - thanks for that. –  Hippyjim Oct 12 '12 at 20:35

2 Answers 2

up vote 6 down vote accepted

One solution would be to simply add custom (e.g. inline) validation rules for the driver attribute of your cars:

public function rules() {
    return array('driver', 'validateDriver');

public function validateDriver() {
    if (!$this->driver->isFoo) {
        $this->addError('driver', Yii::t('validation', '{attribute} is not foo!'));
share|improve this answer
Thbanks - doesn't really help though - I need to access the property of the parent model from the child, to decide how to validate. –  Hippyjim Oct 12 '12 at 20:35
@Hippyjim: Why? It seems to me (although without having much information) that it is not only technically possible but also more appropriate for the car to validate its driver. –  Jon Oct 12 '12 at 21:42
Sorry Jon, I totally misunderstood what you were showing me - having tried it - that's just what I needed - I thought you were treating the driver as an attribute - when it's a model. It never occurred to me you could trigger a validation rule for a child model that way - that solution is perfect. Thanks! –  Hippyjim Oct 12 '12 at 23:42

If I understood your problem correctly you want this

In your Driver model add the relation as

'car' => array(self::BELONGS_TO, 'Car', 'car_id'),

And to check which type of Car belongs to a Driver use instanceOf operator. For example

if($model->car insatnceOf RaceCar)
    echo 'Its a racecar';
elseif($model->car instanceOf LuxuryCar)
    echo 'Its a luxury car';
share|improve this answer
This wont work because the instance of car will always be a Car, the second parameter to a relation is the model class to use. –  Luke Oct 12 '12 at 8:26
@Luke it WILL work. I tested it right now. i used yiiframework.com/wiki/198/single-table-inheritance . Did you overloaded instantiate() method? –  dInGd0nG Oct 12 '12 at 9:25
Sorry about that, you're right. Thanks for the link. –  Luke Oct 12 '12 at 9:27
That would do it - but isn't it very intensive on the database to keep having to load the same model over and over? I've already loaded the info, I just need to access it in the child model. Also, I'm not really checking what kind of car model it is - now I've fixed the editing that made the question not make sense, hopefully you see that. –  Hippyjim Oct 12 '12 at 20:36

Your Answer


