This deals (partly) with the different cases where relations() are used and gets the real values only when they are available.
As long as the relations() method is called after fetching the specific record, you may be able to do what you which to do inside the relations.
Creating a method that does the same job is generally a better way.
My code below allow the use of the relation with complex conditions.
public function relations()
{
if($this->getScenario()===null||$this->getScenario()===''||$this->getScenario()==='search') {
$t=$this->getTableAlias(false,false);
$ds=$this->getDbConnection()->getSchema();
$lat=$ds->quoteColumnName("$t.lat");
$lon=$ds->quoteColumnName("$t.lon");
} else {
$lat=CHtml::value($this->lat);
$lon=CHtml::value($this->lon);
}
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'device' => array(self::BELONGS_TO, 'Devices', 'device_id','joinType'=>'INNER JOIN'),
'areas' => array(self::HAS_MANY, 'GeoArea',array(),'scopes'=>array('contains'=>array($lat,$lon))),
); }
you would not be able to access the $this->getAttribute(‘attributename’) model has been initialized at that point basically the model object is not created
a. Keep the vardump inside ‘relations’ - to keep it simple do CVarDump::dump($this->attributes) .
b. Add a print statement just before your $user->experience (example: print "BEFORE CALL TO EXPERIENCE" ).
If the attributes are printed before the text printed in ‘b.’, the relations() are called before the call to ‘b.’ - my guess is that this is what happens for you.
I also think that if you do ‘$user=User::model()->findByPk(1)’ and then use ‘$user->experience’, that you might have some success in your endeavor (supposing that the user with primary key 1 exists).