phpormkohanaquery-builderkohana-orm

Kohana query builder return


We have a CMS written in Kohana 2.3.x (yes, we know it is an old one). When I use the query builder like this:

$obj = ORM::factory('product')->where(array_of_wheres)->find_all()

than the $obj will be an ORM_Iterator.

But when I write something like this:

$obj = ORM::factory('product');
if($something)
    $obj->where(array_of_wheres);
else
    $obj->where(array_of_other_wheres);
$obj->find_all();

Than the $obj will be a Product_Model instead of ORM_Iterator.

Can someone explain why is this happening?

Thanks, Dave.


Solution

  • In first example $obj is result of find_all() function that is a ORM_Iterator called by on anonymous object of Product Model.

    But in 2nd example $obj is ORM object for Product Model. When you call find_all function it returns result as ORM_Iterator it does not modify original object.

    Here is correct way to do it

    $model = ORM::factory('product');
    if($something)
        $model->where(array_of_wheres);
    else
        $model->where(array_of_other_wheres);
    $obj = $model->find_all();