2013-06-02 45 views
0

我遇到了使用idiORM类的问题。 (文档在这里找到:http://idiorm.readthedocs.org/en/latest/idiORM count()问题

我想获得某个查询的计数,同时保留原来的查询进一步处理,所以我只是将原始查询复制到第二个变量,但似乎尽快我执行count()方法,他将所有内容都复制到另一个变量中。

比如我创建一个查询并保存返回的ORM对象转换为可变$ormResults并将其复制到$copy

$ormResults = ORM::for_table('department')->where('company', '4'); 
$this->_orginalResults = $ormResults; 

// Copy the ORM object into $copy 
$copy = $ormResults; 
$this->resultCount = $copy->count(); 

直到这里它工作正常,预计计结果被正确地存储在$this->resultCount。但是,当我现在var转储(到目前为止)未使用的变量$this->_orginalResults它还包含count()属性,这让我感到困惑,因为我根本没有使用它。

protected '_resultColumns' => 
array 
    0 => string 'COUNT(*) AS `count`' (length=19) 

当我尝试执行$this->_originalResults->findMany();时会造成麻烦。 这是因为count()方法返回ORM对象吗?据我所知PHP代码不会向上传播..是吗?

所以铊;博士:

$test = ORM::forTable('departments')->where('company', '8'); 
$test2 = $test; 
// Works 
var_dump($test->count()); 

// Fails 
var_dump($test2->findMany()); 

但是这工作完全正常:

$test = ORM::forTable('departments')->where('company', '8'); 
$test2 = ORM::forTable('departments')->where('company', '8'); 

var_dump($test->count()); 
var_dump($test2->findMany()); 

任何有识之士/帮助将不胜感激。

回答

1

好吧,弄明白了,一些静态变量正在破坏它。

使用clone复制对象的作品完美。

$test = ORM::forTable('departments')->where('company', '8'); 
$test2 = clone $test; 

var_dump($test->count()); 
var_dump($test2->findMany()); 

的伎俩