清理DooPHP生成的阵列

时间:2013-03-16 12:00:21

标签: php doophp

我使用DooPHP作为我的Json服务,一切正常,除了1个问题: DooPHP的Mysql Orm产生了不干净的结果(我尝试了所有配置)。

我有一个模特:

public $id;
public $name;
public $content;
public $image_small;
public $image_large;
public $category_id;
public $orderday;
public $creat_time;
public $_table = 'recipe';
public $_primarykey = 'id';
public $_fields = array('id','name','content','image_small','image_large','category_id','orderday','creat_time');

我写这样的查询:

$food = Doo::db()->find('Recipe',array('select' => 'id, name, image_small, image_large,category_id'));

但是数组中的结果:(SELECT id,name,image_small,image_large,category_id FROM recipe)

 Array
 (
  [0] => Recipe Object
    (
        [id] => 1
        [name] =>  Chicken
        [content] => 
        [image_small] => image_small/0872c3146bc78b74a006b41353aab13c.jpg
        [image_large] => image_large/0872c3146bc78b74a006b41353aab13c.jpg
        [category_id] => 1
        [orderday] => 
        [creat_time] => 
        [_table] => recipe
        [_primarykey] => id
        [_fields] => Array
            (
                [0] => id
                [1] => name
                [2] => content
                [3] => image_small
                [4] => image_large
                [5] => category_id
                [6] => orderday
                [7] => creat_time
            )

    )

[1] => Recipe Object
    (
        [id] => 3
        [name] => Bak Kut Teh 2  
        [content] => 
        [image_small] => 
        [image_large] => 
        [category_id] => 4
        [orderday] => 
        [creat_time] => 
        [_table] => recipe
        [_primarykey] => id
        [_fields] => Array
            (
                [0] => id
                [1] => name
                [2] => content
                [3] => image_small
                [4] => image_large
                [5] => category_id
                [6] => orderday
                [7] => creat_time
            )

    )

 )

我想删除所有不相关的速度数据(_fields,_table ..),而不选择:content,creat_time ......

2 个答案:

答案 0 :(得分:1)

方法find返回模型对象或查询结果的关联数组。试试这个:

$food = Doo::db()->find('Recipe',array(
'select' => 'id, name, image_small, image_large, category_id',
'asArray' => TRUE));

答案 1 :(得分:0)

将此函数添加到DooController.php

的替代方法
 /**
     * remove unused parameters from query object 
     * @param object $obj models object
     * 
     */
 public function sanitizeQuery($obj){


 foreach($obj as $k){

unset($k->_fields);

unset($k->_primarykey);

unset($k->_table);


}
return $obj;
     }

然后在检索“集合”后的控制器操作中执行此操作:

$food = Doo::db()->find();
$this->data['food'] = $this->sanitizeQuery($food);