繁体   English   中英

Codeigniter WanWizard DataMapper all_to_array错误?

[英]Codeigniter WanWizard DataMapper all_to_array error?

使用WanWizard的DataMapper 1.8.2时出现问题。 我想在get()之后得到一组结果。 下面的代码片段显示了我如何做到这一点与我希望做到的程度,以及据文档所声称的应该做到这一点。

        $arr = $loc->get();
//  THIS WORKS
//        $results = array();
//        foreach($arr as $item) {
//            $results[] = $item->stored;
//        }
        $results = $arr->all_to_array();  // THIS DOES NOT WORK
        $results = $loc->all_to_array();  // THIS ALSO DOES NOT WORK

当我使用“ $ results = $ arr-> all_to_array();”时 调用时,将引发异常。 这是syslog中的异常输出:

PHP Fatal error:  Uncaught exception 'Exception' with message 'Unable to call the method "all_to_array" on the class Location' in /var/www/imhere/application/libraries/datamapper.php:1188\nStack trace:\n#0 /var/www/imhere/application/controllers/locations.php(66): DataMapper->__call('all_to_array', Array)\n#1 /var/www/imhere/application/controllers/locations.php(66): Location->all_to_array()\n#2 [internal function]: Locations->index_get()\n#3 /var/www/imhere/application/libraries/REST_Controller.php(424): call_user_func_array(Array, Array)\n#4 /var/www/imhere/application/libraries/REST_Controller.php(411): REST_Controller->_fire_method(Array, Array)\n#5 /var/www/imhere/system/core/CodeIgniter.php(325): REST_Controller->_remap('index', Array)\n#6 /var/www/imhere/index.php(208): require_once('/var/www/imhere...')\n#7 {main}\n  thrown in /var/www/imhere/application/libraries/datamapper.php on line 1188

还有其他人处理过吗?

其次,我要使用all_to_array方法的原因是我正在使用带有别名的select函数,但是当我使用$ item-> stored时却没有显示。 使用all_to_array返回的值不是显式数据库列吗?

您需要确保已加载array扩展: http : //datamapper.wanwizard.eu/pages/extensions.html

您可以在DataMapper配置文件中全局执行此操作:

$config['extensions'] = array('array');

或在模型本身上:

class Location extends DataMapper {

    var $extensions = array('array');

    // ...
}

或在运行时动态地:

$location = new Location();
$location->load_extension('array');
$results = $location->get()->all_to_array();

那将消除错误!

要回答第二个问题:您实际上不需要访问stored属性,也不需要将结果作为数组返回。 使用面向对象的编程! :)

实现目标的最佳方法是将结果保留为对象,然后在遍历它们时,只需调用您要使用的属性即可。 然后,您可以使代码保持整洁:

$locations = new Location();
$locations->get();
foreach( $locations as $location ) {
    echo $location->property;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM