JOIN option for STAT relation

I have two tables Content and Post. They connected with each other by ‘contentId’ key.

I have model User and I would like to count user’s content (‘userId’ key in Content). But I also need to check if Post.type=1 for each user’s content.

I added ‘contentCount’ relation to relations():


        

function relations()

{

   return array(

      'contentCount'=>array(self::STAT, 'Content', 'userId',

         'condition'=>'t.status=1 AND Post.type=1',

         'join'=>'Post ON Post.contentId=t.contentId',

      ),

   );

}



It creates such SQL:




SELECT `userId` AS `c`, COUNT(*) AS `s` FROM

`Content` WHERE (status=1 AND Post.type=1)

AND (`Content`.`userId`=1) GROUP BY `userId`)



Of course I get such error: Unknown column ‘Post.type’ in ‘where clause’.

Why there are no JOIN condition in SQL statement?

How should I use ‘join’ option in relations.

My Yii v.1.1.4.

Same issue… No JOIN in the generated SQL… :huh:

Is that the correct way to declare a STAT relation?




public function relations() {

            //...  

            'bookings' => array(self::STAT, 'PurchaseDetail', 'ticket_id',

                'join' => 'INNER JOIN Purchase ON purchase_id = Purchase.id',

                'condition' => 'purchase.expiration_date > NOW() OR purchase.purchase_date IS NOT NULL',

            )

        );

    }



If I have




$ticket = Ticket::model()->findByPk(1);

$bookings = $ticket->bookings;



The result is:




//Error in querying SQL:


SELECT `ticket_id` AS `c`, COUNT(*) AS `s` 

FROM `PurchaseDetail` `t` 

//JOIN???

WHERE (purchase.expiration_date > NOW() OR purchase.purchase_date IS NOT NULL) AND (`t`.`ticket_id`='13') 

GROUP BY`ticket_id`