Relations On New Record
Posted 08 January 2013 - 06:28 AM
I'm trying to pull a relation of a new record, mainly for validation purposes, but it will never return anything as it's a new record.
It's hard coded into CActiveRecord that unless you pass the refresh parameter along with it, HAS_MANY and HAS_ONE relations will return blanks.
Why is that? It's a little annoying as it means I can't create a model, lookup some relations on it, and validate it. I have to have special cases in for new records, which just go around the problem by setting isNewRecord to false, then back to true after it's used.
I just can't see a good case for not returning relevant records?
Posted 08 January 2013 - 07:22 AM
It seems more sensible to me to only allow access to the relations once the record has been fully created, rather than having to explain the caveats inherent in accessing them. Your workaround is essentially you taking responsibility for accessing the relations in an unsupported way.
Posted 08 January 2013 - 08:32 AM
$obj = new Thing; $obj->area_id = 10; $obj->jo...... ..... $area = $obj->area;
So if I hadn't set an area ID I could expect no areas.
I can understand any relations defined as [code]'together'=>true/code] returning blanks as they wouldn't be able to come down together.
There's just doesn't seem to be anything unexpected that would happen by allowing it. So I'm struggling to understand why it's not.
I'll leave it a day or so on here to see if someone knows a good reason why they shouldn't be, then I'll open up an Issue on GitHub to enable it and it can be discussed there.