I am a Yii beginner and I am wondering, because the following MySQL query on the command line (not in Yii) retrieves data even if the foreign key composer_Id is NULL:
SELECT * FROM `tbl_main` AS t
LEFT JOIN tbl_related AS rel
ON t.composer_Id = rel.Id
Using relations in Yii ActiveRecord I get an error, when there are records having an empty foreign key.
Hello. The way I handle such situation is to always check if the relation does not return null. You can check it here. Or if you are using CGridView, you can use it like this :
One technique is to use the CHtml::value() method to fetch the value from the model (and hence from the database) - this method implies testing for null.
Well Keith, thanks for the heads up - however I do not know why ;-). I’ve just seen it enough times when going through the inner workings of Yii.
Do you know about my extension RelatedSearchBehavior? I use CHtml::value() in there too. Besides making fields from relations searcheable and sortible, it also provides aliases to related fields for which CHtml::value() is used. Even with your 'Master Member" status, I think you’ll like the extension!