【问题标题】:Silverstripe convertDataObjectSet not workingSilverstripe convertDataObjectSet 不工作
【发布时间】:2013-09-19 13:30:14
【问题描述】:

我有以下代码:

$data = DataObject::get('Property',"SoftDelete=0 AND Bedrooms >= ".$minBeds." AND Price<='". $maxPrice."'","Price ASC");
$f1 = new JSONDataFormatter(); 
return $f1->convertDataObjectSet($data); 

但是响应是:

{"totalSize":null,"items":[]}

DataObject里面肯定有记录,好像我有:

$data = DataObject::get('Property',"SoftDelete=0 AND Bedrooms >= ".$minBeds." AND Price<='". $maxPrice."'","Price ASC");
foreach($data as $dataobj){
print_r($data);
}

我可以看到所有记录的数据。

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

    在迭代之前,ORM 不会实际执行查询,这解释了为什么在使用 foreach 循环 $data 时存在结果,而不是仅仅编写 get(...) 语句。

    一种解决方案是在您的 DataList 上使用 toArray() 方法,然后该方法将执行查询并将结果保存在一个数组中:

    $data = DataObject::get('Property',"SoftDelete=0 AND Bedrooms >= ".$minBeds." AND Price<='". $maxPrice."'","Price ASC")->toArray();
    

    (请注意,如果没有结果,这可能/将会抛出错误,因此可能需要先检查-&gt;count()

    convertDataObjectSet() 似乎将SS_List 作为参数,因此您可能必须像$data = ArrayList::create( $data ) 那样转换$data(不确定)。

    【讨论】:

    • 得到了同样无效的回应,但这是关于许可的。添加适当的 canView 函数,如 public function canView($member = null) { return true; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多