We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Understanding custom custom resultset

Hi,

I am going through docs, while reading this : https://docs.phalcon.io/en/3.3/db-models , surely I understand most of the aspect but can anyone explain custom resultset which is added to a model using getResultsetClass() . I would like to see a working practical example if there is any already .

Thanks

A simple example would be to have a custom resultset for the car model. Where you constantly need to know which is the oldest and the newest car. Then you could have a custom resultset that already has the functions included, getNewest()and getOlder().

I hope it helps you. Good luck



8.8k

A simple example would be to have a custom resultset for the car model. Where you constantly need to know which is the oldest and the newest car. Then you could have a custom resultset that already has the functions included, getNewest()and getOlder().

I hope it helps you. Good luck

So, it means in getNewest() method, we can have query to find latest car by date or when they were added. I was actually looking for some example with working code.

Thanks anyways.. :)

edited Jun '18

getNewest() should not make a query because it is already part of the result. You should use the results to get the newest. Eg

class Custom extends \Phalcon\Mvc\Model\Resultset\Simple {
    public function getNewest() {
        if (!$this->count()) {
            return false;
        }

        $year = $newest = null;
        foreach($this as $element) {
            if ($element->getYear() > $year) {
                $newest = $element;
            }
        }

        return $newest;
    }
}

Good luck

It means that in custom resultset classes you can add your own methods to filter result however you want, return it however you want etc. When using phalcon resultset\simple or resultset\complex you can only use methods which those classes provide.