The solutions you’ve provided would create a completely separate object - not populate the attributes of the class while it’s being constructed.
I might need to clarify what I’m doing. I’m trying to take an large codebase that was not developed with Yii and make it work with Yii’s Active Record.
Currently there are a lot of legacy model classes. They all extend one ORM class - let’s call it: DataObject
So Person extends DataObject.
Rather than recoding all the legacy models - and the code that uses those models - to work with ActiveRecord and Yii - I thought I could just recode DataObject - so that it becomes a wrapper for ActiveRecord. DataObject would now extend ActiveRecord - any old methods in DataObject would be rewritten to use ActiveRecord calls and all the old code would still work, but I’d be able to start using the existing models in Yii (because they now extend ActiveRecord, through this DataObject class).
In our old DataObject class, you could pass the id of a record to the constructor like this:
$p = new Person(1253);
and it would build the new Person object with the data for record #1253 already populated. Going forward, you’re right, we’ll use the normal active records calls to do this. But the old code has to work - and I don’t want to have to rewrite all of it at once.
So how can I override the CActiveRecord constructor so that when the old code passes the id to the constructor, it’ll create an object with the data for that row populated.
The current issue I’m having is that I can’t seem to assign ActiveRecord attributes in the overridden constructor.
class DataObject extends CActiveRecord {
public function __construct($arg) {
parent::__construct($scenario);
if(!empty($arg)) {
$this->id = $arg;
$this->refresh();
}
}
}
Yii bails without an error on
$this->id=$arg
Is there some other way to look up the values of an object from within the constructor?
I think the solution for this might be helpful to others who are trying to phase Yii in to their existing codebase.