Question

I wrote in my controller a conditional filter working like this:

$this->_view = View::factory('crud/index')
    ->bind('collection', $collection);

$collection = ORM::factory($this->_model);
if (Request::current()->method() === Request::POST)
{
    foreach (Request::current()->post('filter') as $field => $value)
    {
        $collection->where($field, '=', $value);
    }
}
$collection->find_all();

And in the view I have a conditional to display a message if there are no filtered results or rows in database.

<?php if ( ! $collection->count()): ?>

This gives me an exception:

Kohana_Exception [ 0 ]: Invalid method count called in Model_Product

The problem is that before adding the filter, my controller action was:

$this->_view = View::factory('crud/index')
    ->bind('collection', $collection);

$collection = ORM::factory($this->_model)->find_all();

And $collection->count() worked just fine in the view. Why is the ORM find_all() method returning a model even if I don't post, even if the code is not entering the conditional? Just breaking $collection = ORM::factory($this->_model)->find_all(); into $collection = ORM::factory($this->_model); and $collection->find_all(); breaks the whole thing. Why is that strange behavior? Thanks.

Was it helpful?

Solution

Try doing this:

$collection = $collection->find_all();

find_all() doesn't save the query results in the ORM object, you need to save it in a variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top