has_many having count > 1

Hi. I have a model Author that HAS_MANY Books . Each Book HAS_MANY Likes and HAS_MANY Readers.

I want to to fetch only those books that have more then one Like and more than one Reader.

I’m trying to do so:




$criteria = new CDbCriteria(array(

   'with' => array(

       'books' => array(

          'with' => array('likes', 'readers'),

          'together' => false,

          // what's here ??

          'having' => 'COUNT(likes) > 0 AND count(readers) > 0' // ??

       )

   )

));

$author = Author::model()->findByPk($id, $criteria);



Likes and readers are HAS_MANY relations, not STAT

You need a group by somewhere imo.




$criteria = new CDbCriteria(array(

   'with' => array(

       'books' => array(

          'with' => array('likes', 'readers'),

          'together' => true,

          'group' => 'books.id',

          'having' => 'COUNT(likes.id) > 0 AND count(readers.id) > 0'

       )

   )

));

$author = Author::model()->findByPk($id, $criteria);



I have two tables, one of clients and other charges, I wanted to know how to return only clientes with two charges in arrears? Winning something like ‘charges.date < \’’ . date(‘Y-m-d’) . ‘\’’