exclude(Q::any(array( 'ispublic'=>Category::VISIBILITY_PRIVATE, Q::all(array( 'faqs__ispublished'=>FAQ::VISIBILITY_PRIVATE, 'children__ispublic' => Category::VISIBILITY_PRIVATE, 'children__faqs__ispublished'=>FAQ::VISIBILITY_PRIVATE, )) ))) //->annotate(array('faq_count'=>SqlAggregate::COUNT('faqs__ispublished'))); ->annotate(array('faq_count' => SqlAggregate::COUNT( SqlCase::N() ->when(array( 'faqs__ispublished__gt'=> FAQ::VISIBILITY_PRIVATE), 1) ->otherwise(null) ))) ->annotate(array('children_faq_count' => SqlAggregate::COUNT( SqlCase::N() ->when(array( 'children__faqs__ispublished__gt'=> FAQ::VISIBILITY_PRIVATE), 1) ->otherwise(null) ))); // ->filter(array('faq_count__gt' => 0)); if ($categories->exists(true)) { ?>