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