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

Why are methods like setReadConnectionService from Phalcon/mvc/model not static?

Hello, everyone. This is an architecture question rather than a troubleshooting one.

As far as I understand, classes that implement Phalcon's ModelInterface are supposed to roughly represent a table in a database, while instances of such classes are supposed to represent a row in the database.

This works quite well; each instance has a bunch of properties which map to columns in the database, and there are static methods that one can use to get rows from the database, such as "find" and all the "findBy(...)" static methods. Also, I'm pretty sure the table name can be derived from the class name, which again, makes quite a lot of sense.

On the other hand, it seems to me that there are a lot of instance methods that should actually be static methods. For example, "setConnectionService". It's odd enough that an instance method can set a global value in the Di (which means this isn't supposed to be used for a one-shot connection to some different database). But more than that, the ModelManager will actually get the class from the Model instance and associate a connection with its class rather than with the instance, which is another good reason why this should be a static method.

My argument is pretty much the same for getSchema, setSchema, getSource, setSource, and probably a few more.

I appreciate any information you guys can give me.

Hi @tomaz-fogaca I implement a singleton pattern to solve this

Good luck

Hello, Emilio

Sure, we can use a singleton pattern, but it seems to me that that violates the original intention of the Model class. It doesn't really make much sense to have a single Model instance around, say, a singleton Robot instance, since once you query the database it'll return a ResultSet which represents a collection of Robots.