1

我将 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 ...

4

2 回答 2

1

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

$food = Doo::db()->find('Recipe',array(
'select' => 'id, name, image_small, image_large, category_id',
'asArray' => TRUE));
于 2013-03-16T12:42:24.580 回答
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);
于 2013-09-12T08:41:13.557 回答