PHP code example of tallieutallieu / dry-dbi

1. Go to this page and download the library: Download tallieutallieu/dry-dbi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tallieutallieu / dry-dbi example snippets




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)
    {
        //
    }
}



namespace Repository;

use Tnt\Dbi\BaseRepository;
use Tnt\Dbi\Criteria\OrderBy;
use Tnt\Dbi\Criteria\IsTrue;
use Tnt\Dbi\QueryBuilder;
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;
    }
}



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();
    }
}