Skip to content

Commit

Permalink
added style to load instance of model
Browse files Browse the repository at this point in the history
  • Loading branch information
agungsugiarto committed Sep 14, 2020
1 parent 2ab1a43 commit 0f0781d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
```
Expand Down
17 changes: 10 additions & 7 deletions src/Eloquent/RepositoryAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
);
}
}
11 changes: 11 additions & 0 deletions src/Scopes/ScopeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down

0 comments on commit 0f0781d

Please sign in to comment.