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

static methods in models?

Hello,

first, thanks for the great framework! I like the simplicity of this framework and the freedom of writing code very much.

But I have a question regarding best practices to remain my increasing code clear.

Now I use often static functions in my models, like this:

... in my database model "receipts"

public static function getCashdesks()
{
    $rows = self::find(array("columns" => "distinct cashdesk"));
    $return = array();
    if($rows->valid()) {
        foreach($rows as $row) {
            $return[] = $row->cashdesk;
        }
    }
    return $return;
}

I have read that a big controller is not prefereneced by a lot of programmers so I do this in my models. It works but I am not happy with that.

Where is the best place to do things like this. I can imagine:

  • leave this static functions in the model

  • create an overall (static) class

  • blow up the controller

Please help me find the right place.. I am always missing "something" between my controllers and my models, but I can't find a spanning item/class to consolidate.

Thank you, Martin

edited Jun '16

Whatin seven hells is this:

$return = array();
    if($rows->valid()) {
        foreach($rows as $row) {
            $return[] = $row->cashdesk;
        }
    }
    return $return;

Why just don't do return $rows->toArray(); ?

In my current project i just created repository where i put all methods, in most of themim calling modelsManager, but sometimes static method too beacause it's cleaner.

Thanks for your answer. Yes, your way is much cleaner! I can't remember why I did this so difficult.

So, you use both, static methods in the database model classes and an own repository for your "app" processes. Anyone else?

I mean in repository i have normal methods where i call static methods from models. But most of the time im using joins so im using modelsManager there.

edited Jun '16

In our projects only models can generate SQL query (or in another way to get data) so we use static method for getting model. If we need query with join - static method will get relationships from modelsManager. And we don't use find or findFirst method from outside of model.

But it's good with simple object. If we want to manage object which consist of several models/tables we write service. It encapsulate some storage logic which work with more than one model/table.... or for optimization SQL ))

It's give us end list of way to get data so we can test code, make smarty cache or replace some model/service to another with same interface - and everything will work.



125.8k
Accepted
answer

If I were you, I'd leave the method where it is. It makes logical sense (to me) that the Receipts model should be the location of a method that retrieves receipt information.

Putting this method in a controller is useless if you need this method in a different controller. Besides, a controller shouldn't be in charge of extracting information from a model - that's the model's job.