0

我在下面有一个代码来查找所有学生列表

$fub = $this->Customer->find('all', array('conditions' => array('Customer.customers_types' => 'student')));

我在下面有一个代码来计算尝试测试的学生人数。

$totalTests = $this->Customer->Test->find('count', array('conditions' => array('Test.customer_id' => $fub['Customer']['id'])));

$this->set('totalTests', $totalTests);

但是,我有不止一位学生客户。如何使用 $fub 的结果并将其放入 $totalTests 条件以获取尝试测试的学生总数。

调试器::dump($fub);

array(
    array(),
    array(),
    array(),
    array(),
    array(),
    array()
)
4

2 回答 2

0

在您的计数查询中,您尝试的条件可能不匹配并返回空数组

$fub['Customer']['id']

是空的。在您的计数查询之前首先调试 $fub 。我遵循的方式是设置

$fub['customer']['id']

变成一个变量,就像这样

$f = $fub['customer']['id']

在您的计数查询之前并将此变量放在您的条件中

$totalTests = $this->Customer->Test->find('count', array('conditions' => array('Test.customer_id' => $f)));

试试这个。

于 2013-01-30T04:41:36.377 回答
0

您可能应该使用 [ CakePHPs counterCache ]。它会为您跟踪相关项目的数量。

于 2013-01-30T03:12:04.427 回答