I have the following Code:
Controller
class UserController {
public function __construct(userRepository $userRepository) {
$this->userRepository = $userRepository;
}
[...]
Repository
class UserRepository extends AbstractRepository {
public function getTablename() {
return "tbl_users";
}
public function getModel() {
return "administration\\CMR\\UserModel";
}
[...]
AbstractRepository
abstract class AbstractRepository {
protected $pdo;
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
abstract public function getTablename();
abstract public function getModel();
function readAll() {
$table = $this->getTablename();
$model = $this->getModel();
$stmt = $this->pdo->query("SELECT * FROM $table");
$res = $stmt->fetchAll(PDO::FETCH_CLASS, $model);
return $res;
}
[...]
My Problem is, that I need the "tbl_users" for one query and a secound table (tbl_locations) for another query. Could anyone please explain how to do this? I think it's unnecessary to write again the same readAll()-Function only with other variable.
Start by adding location repository
class LocationRepository extends AbstractRepository implements LocationRepositoryInterface {
public function getTablename() {
return "tbl_locations";
}
public function getModel() {
return "administration\\CMR\\LocationModel";
}
[...]
and put both repositories to upper layer, say UserLocationService
.
class UserLocationService implements UserLocationInterface {
public function __construct(
protected UserRepositoryInterface $userRepository,
protected LocationRepositoryInterface $locationRepository
) {
}
[...]
}
This way you have separate repositories for users and locations, yet you can operate on both, in Service that does require both repositories to fulfill its' logic.