Posts

Showing posts from June, 2013

Get "Null or Value" on related model in Yii

I have two tables, one with bases and one with transmitters. Bases work without transmitters, but transmitters don't work without bases. This means a HAS_MANY / BELONGS_TO relationship works out well in most cases. I recently ran into a two-fold problem. 1) I wasn't getting the null values from the related table when filtering on !=7. 2) If I added an OR statement, it ran really... really... really slow. The second was easily fixed by adding an index containing the joining field and the filtering field, but that still left the OR, which wasn't real efficient. The solution was coalesce in my search() function. $criteria->addCondition('coalesce(transmitters.field,0) != 7'); transmitters was the related table I was filtering on, and field was the field. Basically, anything that's null becomes a 0, so it's not a 7 (and not a null), so it shows up. Nothing too difficult this time, but hopefully it can save someone (probably me in a few weeks) s