squirrelphp / entities-bundle
Symfony integration of squirrelphp/entities - automatic integration of generated repositories for existing entities.
Installs: 3 154
Dependents: 0
Suggesters: 4
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=8.0
- squirrelphp/entities: ^1.0
- squirrelphp/queries-bundle: ^0.14
- symfony/config: ^5.0|^6.0|^7.0
- symfony/dependency-injection: ^5.0|^6.0|^7.0
- symfony/finder: ^5.0|^6.0|^7.0
- symfony/http-kernel: ^5.0|^6.0|^7.0
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.3
- captainhook/plugin-composer: ^5.0
- mockery/mockery: ^1.0
- phpunit/phpunit: ^10.0
README
Integration of squirrelphp/entities into Symfony through bundle configuration, also needs squirrelphp/queries-bundle as a basis for connecting to databases and executing queries.
Installation
composer require squirrelphp/entities-bundle
Configuration
Enable the bundle in your Symfony configuration by adding Squirrel\EntitiesBundle\SquirrelEntitiesBundle
to the list of bundles, and make sure to enable Squirrel\QueriesBundle\SquirrelQueriesBundle
and configure connections through QueriesBundle, which is the basis for this bundle.
Configure the directories where the bundle will look for repositories like this in Symfony / YAML:
squirrel_entities:
directories:
- '%kernel.project_dir%/src'
- '%kernel.project_dir%/possibleOtherDirectory'
It will go through these directories recusively, finding all entities, generating repositories and creating services for the repositories. The details on how to work with the repositories can be found in the documentation for the underlying library squirrelphp/entities.
Overriding table names and connection names
If you are reusing entities from other projects and only want to change the connection name and/or the table name of an entity, you can override it through configuration:
squirrel_entities:
connection_names:
Application\Entity\User: 'postgres_connection'
Application\Entity\Session: 'sqlite_connection'
table_names:
Application\Entity\User: 'differentdatabase.users'
Application\Entity\Session: 'sessions_table'
This can also come in handy if you want to change connection names and table names for testing or development, while the attribute values are for the production system. Just use the fully qualified entity class name as the key. If you specify an empty string as connection name the default connection is used (if a default connection was defined through QueriesBundle
).
Workflow with entities and repositories
At this point you should have already configured squirrelphp/queries-bundle with your database(s).
While developing
- Create entities with attributes as explained in squirrelphp/entities
- Run
vendor/bin/squirrel_repositories_generate --source-dir=src
to generate the repositories for your entities - those will be gitignored (add or change the --source-dir entries depending on where your entities are) - Add the directories of your entities to the
squirrel_entities
configuration so repositories are found and initialized automatically when the Symfony service container is compiled. - Use the generated repository classes as type hints in your code and let Symfony autowire these services.
- Add
vendor/bin/squirrel_repositories_generate --source-dir=src
to your composer.json inscripts
forpost-install-cmd
andpost-update-cmd
(at the top of these lists). Whenever you do an install or update the repositories are recreated.
In production
If you run composer install
in production, just make sure you have added squirrel_repositories_generate
to your composer.json as described in point number 5 in While developing
, so every time you deploy the repositories are recreated.
If you have some specific deployment workflow, you can call vendor/bin/squirrel_repositories_generate --source-dir=src
explicitely to generate the repositories. Just make sure you do it before the Symfony container is compiled (or before clearing it with cache:clear or cache:warmup), otherwise the repository classes will be missing and autowiring code with the repository classes will not work.