diff --git a/README.md b/README.md index 87dfc05..3f3cf7d 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,13 @@ class NewsRepository extends BaseRepository { public function entity() { + // Whatever choose one your style. + return new News(); + // or + return 'App\Models\News'; + // or + return News::class; } } ``` diff --git a/src/Eloquent/RepositoryAbstract.php b/src/Eloquent/RepositoryAbstract.php index 03c69b3..274e68b 100644 --- a/src/Eloquent/RepositoryAbstract.php +++ b/src/Eloquent/RepositoryAbstract.php @@ -2,7 +2,6 @@ namespace Fluent\Repository\Eloquent; -use CodeIgniter\Model; use Fluent\Repository\Scopes\Scopes; use Fluent\Repository\Contracts\ScopesInterface; use Fluent\Repository\Contracts\CriteriaInterface; @@ -96,17 +95,21 @@ public function __get($name) * Resolve entity. * * @return \CodeIgniter\Model - * + * * @throws RepositoryException */ protected function resolveEntity() { - if (! $this->entity() instanceof Model) { - throw new RepositoryException( - "Class {$this->entity()} must be an instance of CodeIgniter\\Model" - ); + $entity = $this->entity(); + + if (is_string($entity)) { + return new $entity(); + } elseif ($entity instanceof \CodeIgniter\Model) { + return $entity; } - return $this->entity(); + throw new RepositoryException( + "Class {$entity} must be an instance of CodeIgniter\\Model" + ); } } diff --git a/src/Scopes/ScopeAbstract.php b/src/Scopes/ScopeAbstract.php index 387e910..fd900d8 100644 --- a/src/Scopes/ScopeAbstract.php +++ b/src/Scopes/ScopeAbstract.php @@ -4,8 +4,19 @@ abstract class ScopeAbstract { + /** + * In your repository define which fields can be used to scope your queries. + * + * @param \CodeIgniter\Database\BaseBuilder $builder + * @return \CodeIgniter\Database\BaseBuilder $builder + */ abstract public function scope($builder, $value, $scope); + /** + * Override mappings key. + * + * @return array + */ public function mappings() { return [];