dietervyncke / dry-dbi
There is no license information available for the latest version (1.0.7) of this package.
Improved DRY databases
1.0.7
2020-10-22 14:50 UTC
Requires
- reinvanoyen/oak: ^1.0.0
README
Improved DRY databases
Index
Installation
composer require dietervyncke/dry-dbi
Usage
Define service provider
<?php namespace app\provider; use Repository\PageRepository; use Oak\Contracts\Container\ContainerInterface; use Oak\ServiceProvider; class AppServiceProvider extends ServiceProvider { public function register(ContainerInterface $app) { $app->set(Repository\PageRepository::class, Repository\PageRepository::class); } public function boot(ContainerInterface $app) { // } }
Repository definition
Extend from BaseRepository for some default behaviour.
<?php namespace Repository; use Tnt\Dbi\BaseRepository; use Model\Page; class PageRepository extends BaseRepository { protected $model = Page::class; /** * Initial method for default actions */ public function init() { $this->addCriteria( new OrderBy( 'sort_index' ) ); } public function visible() { $this->addCriteria( new IsTrue( 'is_visible' ) ); return $this; } /** * Use querybuilder directly for custom actions */ public function types(array $types) { $this->useQueryBuilder(function(QueryBuilder $queryBuilder) use ($types) { $queryBuilder->whereGroup(function(QueryBuilder $queryBuilder) use ($types) { foreach ($types as $type) { $queryBuilder->where('type', '=', $type->id, 'OR'); } }); }); return $this; } /** * Use querybuilder for table joins (left, right, inner) */ public function expertise(Expertise $expertise) { $this->useQueryBuilder(function(QueryBuilder $queryBuilder) { $queryBuilder->leftJoin('project_expertise')->on('project', '=', 'project.id'); }); return $this; } }
Use repository
<?php namespace Controller; use app\container\Application; class pages { public static function get(Request $request) { $app = Application::get(); $pageRepository = $app->get(PageRepository::class); $visiblePages = $pageRepository->visible()->get(); } }