M
M
Maxim2019-02-22 12:31:42
Yii
Maxim, 2019-02-22 12:31:42

Yii2 how to use repository?

Hello. I began to slowly study and implement DDD. Everything seems to be clear, but there is still one question. I have, for example, userServise, the userRepository repository is connected in it, and in this repository is my user model. The repository has methods: save, delete... The
question is: how can I integrate my repository with Yii. With User and UserQuery models? To have the repository method execute $user-save(), $user::find() and $user::find()->active()->male()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Sedyshev, 2019-02-22
@myks92

And what is the difficulty?

class UserRepository {

    public function save(User $user)
    {
        $user->save();
    }

    public function findMale()
    {
        return User::find()->active()->male();
    }
}

All the repository does is abstract you from the specific implementation of getting/storing an entity.
You can even write bare SQL here, even read data from a file.
The main thing is that the method returns exactly what is needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question