【问题标题】:idiORM count() issueidiORM count() 问题
【发布时间】:2013-06-02 11:57:58
【问题描述】:

我在使用 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());

任何见解/帮助将不胜感激。

【问题讨论】:

    标签: php orm idiorm


    【解决方案1】:

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

    使用clone 复制对象效果很好。

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

    成功了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-20
      • 2013-02-06
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2021-10-28
      相关资源
      最近更新 更多