CakePHP在相关模型上查找条件';计数

CakePHP在相关模型上查找条件';计数,cakephp,Cakephp,我有三种型号。类型有多个收集器,收集器有多个位置 我需要得到所有类型的收藏家至少有一个地方。所以我不需要收藏家没有位置的类型 我在收集器模型中没有count字段,因此无法使用counterCache。我可能会弄错,但我认为最好的方法是使用model::afterFind()方法 <?php // type.php (Model) function afterFind($results, $primary) { if ($primary === true) {

我有三种型号。类型有多个收集器,收集器有多个位置

我需要得到所有类型的收藏家至少有一个地方。所以我不需要收藏家没有位置的类型


我在收集器模型中没有count字段,因此无法使用counterCache。

我可能会弄错,但我认为最好的方法是使用model::afterFind()方法




<?php
// type.php (Model)
function afterFind($results, $primary) {
    if ($primary === true) {
        foreach ($results as $key => $type) {
            foreach ($type['Collector'] as $collector) {
                // Assuming you built the associations/tables/etc. correctly and are following cake conventions.
                if (empty($collector['Place']))  {
                    unset($results[$key]);
                    continue 2;
                }
            }
        }
    }
}
?>
<?php
// types_controller.php

// Assuming you're using the containable behavior? If not
// set recursive to 2.
$this->Type->contain(array(
    'Collector' => array(
        'Place'
    )
));
$types = $this->Type->find('all');

?>