简体   繁体   English

Codeigniter WanWizard DataMapper all_to_array错误?

[英]Codeigniter WanWizard DataMapper all_to_array error?

I'm having an issue using WanWizard's DataMapper 1.8.2. 使用WanWizard的DataMapper 1.8.2时出现问题。 I want to get an array of results after get(). 我想在get()之后得到一组结果。 The following snippet shows how I have been able to do it vs. how I want, and as far as the documentation has claimed, should be able to do it. 下面的代码片段显示了我如何做到这一点与我希望做到的程度,以及据文档所声称的应该做到这一点。

        $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

When I use the "$results = $arr->all_to_array();" 当我使用“ $ results = $ arr-> all_to_array();”时 call, an exception is thrown. 调用时,将引发异常。 Here is the exception output in syslog: 这是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

Has anyone else dealt with this? 还有其他人处理过吗?

Secondly, the reason why I want to use the all_to_array method is that I am using a select function with an alias, but it is not showing up when I use $item->stored. 其次,我要使用all_to_array方法的原因是我正在使用带有别名的select函数,但是当我使用$ item-> stored时却没有显示。 Will using all_to_array return values that are not explicit database columns? 使用all_to_array返回的值不是显式数据库列吗?

You need to make sure the array extension is loaded: http://datamapper.wanwizard.eu/pages/extensions.html 您需要确保已加载array扩展: http : //datamapper.wanwizard.eu/pages/extensions.html

You can either do this globally in the DataMapper config file: 您可以在DataMapper配置文件中全局执行此操作:

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

Or on the model itself: 或在模型本身上:

class Location extends DataMapper {

    var $extensions = array('array');

    // ...
}

or dynamically at runtime: 或在运行时动态地:

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

That will get rid of the error! 那将消除错误!

To answer your second question: you shouldn't really need to access the stored property, and you don't need to return the results as an array either. 要回答第二个问题:您实际上不需要访问stored属性,也不需要将结果作为数组返回。 Use Object-Oriented programming! 使用面向对象的编程! :) :)

The best way of achieving what you want to do is to keep the results as objects, then, when looping through them, simply call the property you're after. 实现目标的最佳方法是将结果保留为对象,然后在遍历它们时,只需调用您要使用的属性即可。 Then you can keep the code pretty clean: 然后,您可以使代码保持整洁:

$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