Yii Framework Forum: Relations On New Record - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Relations On New Record Rate Topic: -----

#1 User is offline   paystey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 12-October 11

Posted 08 January 2013 - 06:28 AM

Hi,

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?

Thanks
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,481
  • Joined: 04-March 10
  • Location:UK

Posted 08 January 2013 - 07:22 AM

I suppose that the relations don't logically exist until the record has been saved to the database, so it would be wrong of Yii to allow you to access them. You'd also have inconsistency in accessing the relations, because any that rely on the new record's primary key will fail if the PK is assigned on creation by the database engine.

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.
0

#3 User is offline   paystey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 12-October 11

Posted 08 January 2013 - 08:32 AM

Yes, I agree completely . It just seems to me that all of the issues you mention are still a problem when the record is in the database. The PK must still be assigned, if it's not, you'll get no relations (which would be expected). I'm thinking of a use case like:

$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.
0

#4 User is offline   WebSee 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 27-July 12

Posted 17 March 2014 - 03:50 AM

I have the same problem.
And I found a solution for those who will look in the future.

Try $obj->getRelated('area', true); instead $obj->area;
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users