daycry / doctrine
Doctrine for Codeigniter 4
Installs: 1 982
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 4
Forks: 3
Open Issues: 1
Requires
- php: >=8.2
- beberlei/doctrineextensions: ^1.0
- doctrine/dbal: ^4
- doctrine/orm: ^3
- jms/serializer-bundle: ^4
- symfony/cache: ^7
- symfony/yaml: ^6.2
Requires (Dev)
- codeigniter4/framework: ^4
- friendsofphp/php-cs-fixer: ^3.6
- mikey179/vfsstream: ^1.6
- nexusphp/cs-config: ^3.3
- nexusphp/tachycardia: ^1.0
- phpstan/phpstan: ^1.7.1
- phpunit/phpunit: ^9.1
- rector/rector: 0.18.12
This package is auto-updated.
Last update: 2024-12-21 17:27:53 UTC
README
Doctrine
Doctrine for Codeigniter 4
Installation via composer
Use the package with composer install
> composer require daycry/doctrine
Manual installation
Download this repo and then enable it by editing app/Config/Autoload.php and adding the Daycry\Doctrine namespace to the $psr4 array. For example, if you copied it into app/ThirdParty:
$psr4 = [ 'Config' => APPPATH . 'Config', APP_NAMESPACE => APPPATH, 'App' => APPPATH, 'Daycry\Doctrine' => APPPATH .'ThirdParty/doctrine/src', ];
Configuration
Run command:
> php spark doctrine:publish
This command will copy a config file to your app namespace and "cli-config.php" file for doctrine cli.
Then you can adjust it to your needs. By default file will be present in app/Config/Doctrine.php
.
Usage Loading Library
$doctrine = new \Daycry\Doctrine\Doctrine(); $data = $doctrine->em->getRepository( 'App\Models\Entity\Class' )->findOneBy( array( 'id' => 1 ) ); var_dump( $data );
Usage as a Service
$doctrine = \Config\Services::doctrine(); $data = $doctrine->em->getRepository( 'App\Models\Entity\Class' )->findOneBy( array( 'id' => 1 ) ); var_dump( $data );
Usage as a Helper
In your BaseController - $helpers array, add an element with your helper filename.
protected $helpers = [ 'doctrine_helper' ];
And then, you can use the helper
$doctrine = doctrine_instance(); $data = $doctrine->em->getRepository( 'App\Models\Entity\Class' )->findOneBy( array( 'id' => 1 ) ); var_dump( $data );
Cli Commands
//Mapping de database to entities classes php cli-config.php orm:convert-mapping --namespace="App\Models\Entity\" --force --from-database annotation . //Generate getters & setters php cli-config.php orm:generate-entities . //Generate proxy classes php cli-config.php orm:generate-proxies app/Models/Proxies
If you receive the followrin error: [Semantical Error] The annotation "@JMS\Serializer\Annotation\ExclusionPolicy" in class App\Models\Entity\Secret was never imported. Did you maybe forget to add a "use" statement for this annotation?
You must execute the following command
composer dump-autoload
Using DataTables
Usage with doctrine/orm:
$datatables = ( new \Daycry\Doctrine\DataTables\Builder() ) ->withColumnAliases( [ 'id' => 'qlu.id' ] ) ->withIndexColumn( 'qlu.id' ) ->withQueryBuilder( $this->doctrine->em->createQueryBuilder() ->select( 'qlu.param, q.param, q.param, qs.id as param, qlu.param, qlu.param' ) ->from( \App\Models\Entity\Class::class, 'qlu' ) ->innerJoin( \App\Models\Entity\Class::class, 'qs', \Doctrine\ORM\Query\Expr\Join::WITH, 'qs.id = qlu.*' ) ->innerJoin( \App\Models\Entity\Class::class, 'ql', \Doctrine\ORM\Query\Expr\Join::WITH, 'ql.id = qlu.*' ) ->innerJoin( \App\Models\Entity\Class::class, 'q', \Doctrine\ORM\Query\Expr\Join::WITH, 'q.id = ql.*' ) ) ->withRequestParams( $this->request->getGet( null ) ); $response = $datatables->getResponse(); echo \json_encode( $response );
If you receive an error: Not all identifier properties can be found in the ResultSetMapping you can use:
->setUseOutputWalkers( false )
Example
$datatables = ( new \Daycry\Doctrine\DataTables\Builder() ) ->withColumnAliases( [ 'id' => 'qlu.id' ] ) ->withIndexColumn( 'qlu.id' ) ->setUseOutputWalkers( false ) ->withQueryBuilder( $this->doctrine->em->createQueryBuilder() ->select( 'qlu.param, q.param, q.param, qs.id as param, qlu.param, qlu.param' ) ->from( \App\Models\Entity\Class::class, 'qlu' ) ->innerJoin( \App\Models\Entity\Class::class, 'qs', \Doctrine\ORM\Query\Expr\Join::WITH, 'qs.id = qlu.*' ) ->innerJoin( \App\Models\Entity\Class::class, 'ql', \Doctrine\ORM\Query\Expr\Join::WITH, 'ql.id = qlu.*' ) ->innerJoin( \App\Models\Entity\Class::class, 'q', \Doctrine\ORM\Query\Expr\Join::WITH, 'q.id = ql.*' ) ) ->withRequestParams( $this->request->getGet( null ) ); $response = $datatables->getResponse(); echo \json_encode( $response );
Search
To search from datatables there are nine different search modes
Prefixes are case-insenstive (IN, in, OR, or). Provided search terms were trimmed.
Example
public function testDataTableSearchColumnWithOr() { $doctrine = new \Daycry\Doctrine\Doctrine($this->config); $request = \Config\Services::request(); $datatables = ( new \Daycry\Doctrine\DataTables\Builder() ) ->withColumnAliases( [ 'id' => 't.id', 'name' => 't.name' ] ) ->withIndexColumn('qlu.id') ->setUseOutputWalkers(false) ->withCaseInsensitive(false) ->withColumnField('name') ->withQueryBuilder( $doctrine->em->createQueryBuilder() ->select('t.id, t.name') ->from(\Tests\Support\Models\Entities\Test::class, 't') ) ->withRequestParams( array( 'draw' => 1, 'start' => 0, 'length' => 10, 'search' => array('value' => '', 'regex' => true ), 'columns' => array( array( 'data' => 'id', 'name' => 'id', 'searchable' => true, 'orderable' => true, 'search' => array('value' => '[OR]1,3', 'regex' => false) ), array( 'data' => 'name', 'name' => 'name', 'searchable' => true, 'orderable' => true, 'search' => array('value' => '', 'regex' => false) ) ), 'order' => array( array( 'column' => 0, 'dir' => 'asc') ) ) ); echo $response = json_encode($datatables->getResponse()); }