"through" relation with ActiveRecord failure due to FK exception

Looks like a bug to me, the expected behavior is to replace the ‘on’ clause and instead it ignores it

WIthout using ‘through’, the same works (an empty ‘’ using ‘on’)

Even tho it doesn’t seen right, try this:




return array(

'users'=>array(self::HAS_MANY,'User','fk_customer_id'),

'contributedExecutives'=>array(self::BELONGS_TO,'Executive','fk_created_by_user_id','through'=>'users')

);

If possible try this format




Customer::model()

  ->with('users', 'users.contributedExecutives')

  ->findAll('condition for selecting the one customer');


// or 

Customer::model()

  ->with('users', 'users.contributedExecutives')

  ->together()

  ->findAll('...');


// or more recent style

//(with/together return type was changed from CActiveFinder to CActiveRecord)

Customer::model()->findAll($criteria_containing_with_together_condition);



/Tommy

Tommy – that will return a Customer model with users, right?

I have the customer model already. And I’ve shown code that demonstrates what I want to do – just get Executive models alone, and then manipulate them – display them as a list, sum up the total count, etc.

I’m not seeing how your code makes that happen any easier than what I illustrated above…?

Hi Gustavo,

No, that results in an exception in Yii 1.1.7:

Property "CBelongsToRelation.through" is not defined.

Yes, but also the contributedExecutives belonging to each user.

As you’ve already found out: if you do like this




$customers = $model->with('users', 'users.contributedExecutives')->findAll()



you will get all customers, unless you add a condition.

The executives can be listed this way




$customers = Customer::model()

  ->with('users', 'users.contributedExecutives')

  ->findAll('t.id="some_customer_id"');


foreach ($customers as $customer)

{

  echo 'Customer id: '.$customer->id.'<br/>';

  foreach ($customer->users as $user)

  {

    echo 'User id: '.$user->id.'<br/>';

    foreach ($user->contributedExecutives as $executive)

      echo 'Executive id: '.$executive->id.'<br/>';

  }

}



/Tommy

Yeah I don’t see what that method is going to buy me over what I’ve already figured out…it’s even more verbose…

I am either going to go with the code that I figured out above or get the ‘through’ relation to work, but I’m thinking at this point that the relationship is doomed… :(

I did some testing with a through relationship similar to yours. Got some strange results (actually fewer elements than expected). So I think through is not an option (though I still don’t understand exactly how it’s intended to work).

This is what I would suggest instead (assuming your customer AR object in $model)




$users = User::model()

  ->with(contributedExecutives)  // defined in User model

  ->findAll('t.fk_customer_id="'.$model->id.'"')


foreach ($users as $user)

  foreach ($user->contributedExecutives as $executive)

    echo 'Executive id: '.$executive->id.'<br/>';



or you have to define a BELONGS_TO relation (‘users’) in Executive and access like this




$executives = Executive::model()

  ->with('users')

  ->findAll('users.fk_customer_id="'.$model->id.'"');


foreach ($executives as $executive)

  echo 'Executive id: '.$executive->id.'<br/>';



/Tommy

Preston, did you find an answer to this? I have a similar problem. I’d be interested in knowing what sort of solution you worked out.

Holly,

These last posts were as far as I got on the matter. Either my or Tommy’s solutions work… They really aren’t what I would expect from the framework but it appears Yii as of at least 1.1.8 does not support STAT relations that need a through clause. I’d love for one of the toolkit authors or maintainers to correct me. I should probably file a bug or request for enhancement.

Thanks for the reply, Preston, and for following through on this so diligently. Maybe it will be addressed in the future. I’m glad you found a workaround!

Спасибо!!! Очень помогла тема:)