Singleton Model Teaching

Can we have a model class that is single in the Doctrine?

For Singleton classes, I have to have a private / protected constructor .... but this is not possible, as I am extending the Doctrine class, which has a public constructor

You can argue about using the Singleton template when interacting with the database, but just consider this scenario:

I have a user action log that is logged in the database. This registrar performs some initialization in the constructor (obtaining current user information from the session), which is common to all registrar instances for a specific execution.

There seems to be no way to implement a singleton pattern for models when using Doctrine?

+3
source share
3 answers

An instance of a Doctrine model class corresponds to a single object, for example. the instance Userrepresents one user, and I doubt that you have only one of them. Put your other code in a separate class, UserManageror something else.

class Logger { // plain old singleton class

    function log(x) {
        entry = new LogEntry(x); // LogEntry extends Doctrine_Model
        entry.save();
    }

}
+7
source

You can override the public constructor so that it uses the singleton factory method, which will either create an instance if it does not already exist, or extract an existing instance, and then return the instance to the calling constructor.

+1
source

Doctrine, PHP, (, ). .

0

Source: https://habr.com/ru/post/1752918/


All Articles