how do I use afterConstruct() to add data to my model?

I am just getting tarted, both in Yii & PHP OOP.

I’m following a tutorial by Larry Ullman of a basic setup of employee & department crud pages, working good.

The department table has two fields, id & name.

Now I want to add a new property to the department model, and have it’s value be the concantenated string of two other properties from the same object (id & name).

I thought I could use afterConstruct() method in the class, but I don’t understand what the API means by

"Make sure you call the parent implementation so that the event is raised properly."

in the file models/Department.php

class Department extends CActiveRecord

{

I can add a public property, i.e. idname…

public $idname;

then do I need to write a function to set that properties value?

I think I understand that when the object is created from the class, it should set the properties of the object (id & name) from the values in the database record that is selected by the findAllByPK. That seems to work ok, as my edit form displays the data ok.

But the class Department does show any of that (probably because the class CActiveRecord does all the heavy lifting)

So I don’t know where to call the afterConstruct() method from inside the Department class

I wrote a function inside the Department class:

protected function afterConstruct()

{ $this->idname=‘testing’;

/* then added this from some other forum post, but not understanding what it does */

parent::afterConstruct();

}

but it doesn’t seem to work

Please someone teach me how to do it correctly. thanks

you can add a public function getIdName()

{

return $this->id . ' ' . $this->name;

}

call $modelInstance->IdName or $modelInstance->getIdName()

i accidentally submitted the post before i was finished, then went back in to finsih it. it looks like you responded while I was finishing my post.

your suggestion doesn’t actually add an attribute to the object. please go back and read my edited post to find out what I was trying to do. thanks.