makinacorpus / query-builder-bundle
Symfony integration for makinacorpus/query-builder
Installs: 12
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.1
- doctrine/doctrine-bundle: ^2.10.0
- makinacorpus/query-builder: ^0.3.0|^1.0
- symfony/config: ^6.0|^7.0
- symfony/console: ^6.0|^7.0
- symfony/dependency-injection: ^6.0|^7.0
- symfony/filesystem: ^6.0|^7.0
- symfony/finder: ^6.0|^7.0
- symfony/process: ^6.0|^7.0
- symfony/yaml: ^6.0|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.34
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.3
- symfony/framework-bundle: ^6.0|^7.0
- symfony/validator: ^6.0|^7.0
README
Integrates makinacorpus/query-builder
into Symfony.
Setup
First install:
composer require makinacorpus/query-builder-bundle
Then add the bundle to config/bundles.php
if symfony/flex
did not:
return [ // ... your other bundles. MakinaCorpus\QueryBuilderBundle\QueryBuilderBundle::class => ['all' => true], ];
And you're done.
Services
Each Doctrine connection will have both MakinaCorpus\QueryBuilder\QueryBuilder
and MakinaCorpus\QueryBuilder\DatabaseSession
associated service in container.
They are identifier by the query_builder.session.CONNECTION_NAME
service
identifier. You can manually inject by using the service name, or use autowiring.
You can target a Doctrine connection by injecting a QueryBuilder
or
DatabaseSession
typed service by setting the parameter name to the Doctrine
connection name, for example:
<?php declare (strict_types=1); namespace App\Controller; use MakinaCorpus\QueryBuilder\DatabaseSession; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class TestingController extends AbstractController { #[Route('/testing/query-builder', name: 'testing_query_builder')] public function testQueryBuilder( DatabaseSession $someConnectionName, ): Response { } }
Will have the database session bridged over the some_connection_name
configued Doctrine connection.
Usage
Simply inject the service wherever you need it, a controller action for example:
<?php declare (strict_types=1); namespace App\Controller; use MakinaCorpus\QueryBuilder\DatabaseSession; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class TestingController extends AbstractController { #[Route('/testing/query-builder', name: 'testing_query_builder')] public function testQueryBuilder( DatabaseSession $session, ): Response { $result = $session ->select('some_table') ->executeQuery() ; $data = []; foreach ($result->iterateAssociative() as $row) { $data[] = $row; } return $this->json($data); } }
Basic, simple.