Clean up array produced by DooPHP

338 Views Asked by At

I use DooPHP for my Json service, Everything is ok except 1 problems: Mysql Orm of DooPHP produced unclean result (I had try all config).

I have a Model:

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');

And i write query like this:

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

But the result in array: (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
            )

    )

 )

I want to remove all unrelated data for speed (_fields, _table..) and which not select: content, creat_time ...

2

There are 2 best solutions below

0
On BEST ANSWER

Method find returns a model object or associative array of the queried result. Try this:

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

An alternative method by adding this function to your 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;
     }

then in your controller action after retrieving a "collection" do this:

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