yikes911
(Unique Yikes)
June 8, 2012, 2:03pm
1
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"')
l.jurgs
(L Jurgs)
June 8, 2012, 2:55pm
2
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?
yikes911
(Unique Yikes)
June 8, 2012, 3:14pm
3
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.
l.jurgs
(L Jurgs)
June 8, 2012, 3:55pm
4
You don’t have a member variable defined as messages in MfgProperties do you? That would cause the behaviour you are seeing.
yikes911
(Unique Yikes)
June 8, 2012, 4:11pm
5
No, I don’t. The only instance of ‘messages’ in a Model is a relation name.
yikes911
(Unique Yikes)
June 8, 2012, 4:13pm
6
Disregard that, I do have it in a class it is extended from.