我有一堆extends
abstract BaseRepository
,
我怎样才能让存储库使用baseRepository的代码(Model::find($id);
)和他们自己的模型?我尝试过使用self::setModel(new Model)
但是当我致电find()
时,我收到错误消息称model
不是object
我现在如何得到它:
的 BaseRepository
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Repositories;
use Illuminate\Container\Container;
/**
* Description of BaseRepository
*
* @author Jordy
*/
abstract class BaseRepository implements RepositoryInterface {
protected $model;
function getModel() {
return $this->model;
}
function setModel($model) {
$this->model = $model;
return $this;
}
function findBy($prop, $value) {
return $this->model->where($prop, $value)->get();
}
function findById($id) {
return $this->model->find($id);
}
function getAll() {
return $this->model->all();
}
function insertIntoDatabase($data){
$this->model->create($data);
return $this;
}
}
SurveyRepository
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Repositories;
use App\MbSurvey as Survey;
/**
* Description of SurveyRepository
*
* @author Jordy
*/
class SurveyRepository extends BaseRepository implements RepositoryInterface {
function __construct() {
$this->setModel(new Survey);
}
}
这样做有效,但我想用单例做,而不是new repo()
然后$repo->findById()