Relation as a method

Hi,

What is the difference between accessing related model in those two ways:


$model->relatedModel

$model->relatedModel()

?

I thought that there is no difference, but in my application the second one returns expected results, while the first returns an empty array.

The relation type is HAS_MANY, and is set up as


'messages' => array(self::HAS_MANY, 'MessageBoard', 'refId', 'on'=>'type="property"')

Both of those references call CActiveRecord::getRelated(). If you look at the code, there isn’t any difference. One is called from __get and the other from __call. I don’t know why it is returning different results, they should be exactly the same. Have you tried tracing through in a debugger?

An example where I just dump it.

Whole trace for the first way




TRACE START [2012-06-08 15:11:00]

 0.2002 4069760 -> var_dump() C:\wamp\www\yii\admin\protected\views\users\tabs\accordion\_propertySummary.php:33

 0.2002 4069760 -> xdebug_stop_trace() C:\wamp\www\yii\admin\protected\views\users\tabs\accordion\_propertySummary.php:35

 0.2002 zu

TRACE END   [2012-06-08 15:11:00]

Snippet of trace for the second way:


TRACE START [2012-06-08 15:08:26]

 0.3270 5019536 -> MfgProperties->messages() C:\wamp\www\yii\admin\protected\views\users\tabs\accordion\_propertySummary.php:33

 0.3271 5019688 -> CActiveRecord->__call() C:\wamp\www\yii\admin\protected\views\users\tabs\accordion\_propertySummary.php:33

...

 0.3549 5024336 -> var_dump() C:\wamp\www\yii\admin\protected\views\users\tabs\accordion\_propertySummary.php:33

 0.3549 5024216 -> xdebug_stop_trace() C:\wamp\www\yii\admin\protected\views\users\tabs\accordion\_propertySummary.php:35



So, yeah, the second one does _call(), but the first one just doesn’t do anything.

You don’t have a member variable defined as messages in MfgProperties do you? That would cause the behaviour you are seeing.

No, I don’t. The only instance of ‘messages’ in a Model is a relation name.

Disregard that, I do have it in a class it is extended from.