AR Relations Help


I am trying to set up the AR relations to this ERM (see attached image).

Here is the SQL I would use if it was just an SQL query outside of an AR.

Basically the goal is retrieve a devices current acive price, as a device can have many price versions. In this instance I just want to get the active prices for a specific device.

However I am having problems with the AR setup (mainly because I'm not sure what I'm doing lol).

Here is my current setup:

The call

The device model

The Device Library Prices model

The Device Library Version model

The result of the current setup is that it just gets back any old price version, instead of the active version.

I’m not exactly sure where I am going wrong so any help would be hugly appreciated! :)




wow, this is a lot to read and understand  ;)

What is the SQL generated by yii? I'm sure this would make it easier!


Well after hours upon hours of fiddling, testing, and consulting with colleges, we have come to the conclusion that either A) We can’t fathom how to set up a relation such as this in Yii AR or B) Yii can’t handle a complex relation such as this.

Either way we have switched to just using a SQL statement and calling that as a pose to an AR model, if we can figure out how to set up the relations for the AR then we will but for the moment project deadlines means we can’t spend any more time on this :(

This can easily be done with ActiveRecord. You just need to take more care in defining your JOINs. Use 'on' and 'joinType' apropriately. Try something like this: