我在使用idiORM类时遇到了问题。(文档在这里:http://idiorm.readthedocs.org/en/latest/)
我试图获得某个查询的计数,同时保持原始查询进一步处理,所以我简单地将原始查询复制到第二个变量中,但似乎只要我执行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代码不会向上传播。不是吗?
所以Tl;博士:
$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());
好的,我明白了,显然一些静态变量破坏了它。
使用clone
来复制对象是完美的。
$test = ORM::forTable('departments')->where('company', '8');
$test2 = clone $test;
var_dump($test->count());
var_dump($test2->findMany());