returning properties of children

Hi,

I’ve got a class Person (based on table person), which extends CActiveRecord, and a class Student (based on table student) which extends class Person.

the table person has fields named first_name and last_name.

what I want to do is relatively simple. I wan’t to write a method “getName()” which returns the full name (i.e. first_name concatenated with last_name) for any child class of Person. Something like:




public function getName() {

     return $this->first_name .' '. $this->last_name;

}



But when I call $student->getName();

I get this error: “Student.first_name is not defined” - so presumably, even though Student extends Person, and not CActiveRecord, the magic getter method is looking for the existence of the column “first_name” in table “student” and when it doesn’t find it there, it does not then look in table “person”.

any way around this? any way Yii can handle this in the future without me having to put in a workaround? or have I missed something?

-Charlie

hi,

i think you can use relations in your activerecord models, so you dont have to extend the Person class, the person data will be selected in the query and and will be available as the a Student Attribute, does your database tables have foreign key constraints between them?

heres the documentation http://www.yiiframework.com/doc/guide/1.1/en/database.arr

yes - I do have foreign keys set up and can set up a relation. I was just wondering if there was a way that worked within the class hierarchy - but thanks, yes, I could do it that way.

I’m probably just not understanding how objects work in php, but I though that the active record __get code would handle this… I’m not sure why it wouldn’t work… seems like it’s checking for the existing of the attribute in the class, then, if it doesn’t find it, it passes control to the parent with parent::_get($name). Shouldn’t it then find the field it’s looking for in the parent class?

Here’s the __get method in CActiveRecord:




public function __get($name)

{

    if(isset($this->_attributes[$name]))

        return $this->_attributes[$name];

    else if(isset($this->getMetaData()->columns[$name]))

        return null;

    else if(isset($this->_related[$name]))

        return $this->_related[$name];

    else if(isset($this->getMetaData()->relations[$name]))

        return $this->getRelated($name);

    else

        return parent::__get($name);

}



anyone know why that won’t check all objects in the heirarchy for those attributes and return a value when it finds it?

maybe because the Person class attributes are not loaded when using find then selecting in the database, rather activerecord selects attributes for the Student class which overloads Persons methods and attributes,

yeah, that makes sense. Does it seem like reasonable request (and nice feature) for future Yii versions to load the attributes of a parent class - so that they’re all available in the child?

It doesn’t feel very “object oriented” to treat that information as related, but in a separate object, when it really isn’t in a separate object - it’s just stored that way in the database.

apologies - as this has been discussed before here:

http://www.yiiframework.com/forum/index.php?/topic/5803-my-multi-table-inheritance-approach/

and

http://www.yiiframework.com/forum/index.php?/topic/12978-class-table-inheritance/

and probably elsewhere.

It’d be terrific if Yii could handle this without having to code a workaround.