Complex conditional STAT relation

Hello good people!

I have a problem, and hoping someone can lend me a hand.

I have the following tables/models:

owner (id, name, private, ...)

  • private is boolean

  • owner "has_many" items

item (id, user_id, category_id, private, ...)

  • private is boolean,

  • user_id is fk to owner table

  • category_id is fk to category table

  • item "belongs_to" owner

  • item "belongs_to" category

category (id, title, ...)

  • category "has_many" items

  • category has STAT relation "itemCount" to items

So, here’s what I’m trying to do: at the moment the itemCount returns the total items for each category.

What I’d like is if the website user is NOT logged in (guest), the itemCount for each category should only include items where item.private = 0(false) and owner.private = 0(false). If the website user IS logged in, the itemCount should include items same as above, but also items where they are the owner, eg:

Yii::app()->user->id == items.user_id

The trouble I’m having is specifying for the itemCount relation how to check not just item.private but somehow item->owner.private (that is, the ‘private’ value for the owner of the item). My first aim is to get it working for the guest case. Then I’ll move to the logged in user case.

Anyone able to help me with this?