Kohana ORM count_all() 有效,但 find_all() 无效

问题描述 投票:0回答:3

我遇到一个问题,我正在根据 $_POST 中的多个条件构建 ORM 查询。最终查询看起来不错,并在直接 SQL 查询 (phpmyadmin) 中返回记录,但在我的应用程序中不返回任何记录。这是代码...

        $records = ORM::factory('record')->where(array('date >='=>$_POST['fromdate'],'date <='=>$_POST['todate']));
        if ($_POST['agent'] != '0') $records->where(array('ccp_id'=>$_POST['agent']));
        if ($_POST['supervisor'] != '0') {
            $ccps = ORM::factory('employee')->where(array('supervisor_id'=>$_POST['supervisor'],'active'=>'1'))->find_all();
            foreach ($ccps as $ccp) {
                $agents[] = $ccp->id;
            }
            // echo kohana::debug($agents);
            $records->in('ccp_id',$agents);
        }
        if ($_POST['lead'] != '0') $records->where(array('lead_id'=>$_POST['lead']));
        if ($_POST['reasons'] != '[]') {
            $reasons = explode(',',str_replace(array('[',']','"'),'',$_POST['reasons']));
            $records->in('reason_id',$reasons);
        }
        $records->find_all();

$records->loaded 为 false。如果我用 count_all() 更改 find_all() 我会得到准确的计数。

使用 $_POST 中的示例数据,我在 $records->last_query() 中有此查询

SELECT `records`.*
FROM (`records`)
WHERE `date` >= '2010-10-10'
AND `date` <= '2010-11-09'
AND `ccp_id` IN ('E128092','E128093','E124874','E124414','E129056','E137678','E078952','E112701','E084457','E098047','E099221','E001131','E120892')
AND `lead_id` = 'E110873'
AND `reason_id` IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
ORDER BY `records`.`id` ASC

这会在 phpmyadmin 中返回 4 条记录,而 count_all() 则返回 (4) 条记录。 我不明白为什么会发生这种情况。任何见解都会有帮助。谢谢你。

php orm kohana
3个回答
2
投票

在你的最后一行你应该有

$records = $records->find_all();

而不是

// this actually returns you the resultset and resets the query builder object
$records->find_all() 

0
投票

$records
Database_Result
并且没有
loaded
属性。使用
count($records)
或使用
foreach
语句迭代来获取 ORM 对象。


0
投票

请注意:如果您想使用 $records->count_all(),最好不要清除 ORM 对象( $results = $records->find_all() 而不是 $records = $records->find_all()) ) 或代码中稍后的其他调用。只是我遇到的一个问题。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.