0

For example I need to find a user which its status is 0 and its name is John. So beside usual methods for a Repository pattern (e.g. find, create), I decide to create a method in UsersRepository called findJohnWithStatusZero or to be more generic, a method called findUserWithStatusAndName

UsersRepository.php

<?php

class UsersRepository
{
    public function create(array $data)
    {
        //
    }

    public function find($id)
    {
        //
    }

    public function findUserWithStatusAndName($status, $name)
    {
        return User::where('status', $status) -> where('name', $name) -> first();
    }

}

Controller.php:

...
public function index(UsersRepository $users)
{
    $john_zero = $users -> findUserWithStatusAndName(0, 'john');
}
...

I want to know, if I use these customized methods in my Repository, do I violate the Repository Pattern principles?

Edit: I provided more generic method name instead of findJohnWithStatusZero

  • 1
    Not necessarily, based on my reasoning here. But "findJohnWithStatusZero" sounds oddly specific for a repository method. If it were "GetPremierCustomers" or something like that it would make more sense. – Eric King Oct 24 '18 at 18:39
  • @EricKing I know findJohnWithStatusZero is a bit strange. It is an example. The more realistic names could be findByType, findByRegisterDate, deleteOldPosts – AliN11 Oct 24 '18 at 18:56
  • What is your idea if I use findByType, findByRegisterDate, deleteOldPosts method names? – AliN11 Oct 24 '18 at 18:57
  • The examples you are providing are more like Service Layer or Business Layer methods, not repository methods. – Robert Harvey Oct 25 '18 at 1:20
4

At the repository level I would recommend having a more generic method like Find(User user) or findUserByNameAndStatus(name, status). Getting overly specific like your example is unlikely to be a useful method for re-usability, and it's probably embedding business logic into your repository that is better implemented elsewhere. Specific methods aren't bad in a repository, but you should be able to answer why findJohnWithStatusZero is a method, but not findSaraWithStatusTwo, will you eventually need findBillWithStatusOne? It should generally be obvious why a method in your repository exists.

  • Thanks. I meant something like findUserByNameAndStatus. So you say it is ok to use method names like findUserByNameAndStatus? – AliN11 Oct 24 '18 at 19:08
  • 1
    @AliN11 having multiple findByThing methods is almost expected, its just a balancing act of how many is too many. separate find by name, status and name/status would be too many overlapping methods, but findById and FindByName are probably separate enough. – Ryathal Oct 25 '18 at 16:02
2

This is typically solved using a search() function.

public function search($searchModel)
{
    return User::where('status', $searchModel->status) -> where('name', $searchModel->name) -> first();
}

class SearchModel { 
    public $status = 0; 
    public $name = NULL;
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.