j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

IdiormResultSet #234

Closed kaemu closed 10 years ago

kaemu commented 10 years ago

By using the return_result_sets option, I would have liked to do the following:

 class ParcelModel extends \Model {
    …
    function my_as_array() {
        $a = parent::as_array();
        $a['area'] = $a['width']*$a['height']; // add computed data to my model
        return $a;
    }
}

$myView->data->parcels = Model::factory('ParcelModel')->find_result_set()->my_as_array();

It gives an IdiormMethodMissingException, Method my_as_array() does not exist in class IdiormResultSet

Is there a way for IdiormResultSet to call as_my_array?

treffynnon commented 10 years ago

This does not currently exist as the model that https://github.com/j4mie/idiorm/blob/master/idiorm.php#L2425 gets passed is of type ORM and not your ParcelModel therefore you cannot access any methods defined on ParcelModel, but you can access any of the public methods of ORM.

You would have to define a new method on the result set class in Idiorm that takes a callable and runs it against each item. This is not something that would be merged into Idiorm.

I would just use a function that processes the result set:

class ParcelModel extends \Model {
    public static function my_as_array($result_set) {
        $a = $result_set->as_array();
        $a['area'] = $a['width'] * $a['height']; // add computed data to my model
        return $a;
    }
}
$rs = $myView->data->parcels = Model::factory('ParcelModel')->find_result_set();
$my_array = ParcelModel::my_as_array($rs);
kaemu commented 10 years ago

Thank you for your response