Download the PHP package umanit/doctrine-singleton-bundle without Composer
On this page you can find all versions of the php package umanit/doctrine-singleton-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download umanit/doctrine-singleton-bundle
More information about umanit/doctrine-singleton-bundle
Files in umanit/doctrine-singleton-bundle
Package doctrine-singleton-bundle
Short Description Singleton entities working with Doctrine and Sonata
License MIT
Homepage https://github.com/umanit/doctrine-singleton-bundle
Informations about the package doctrine-singleton-bundle
Doctrine Singleton Bundle
This bundle intends to easily create complex singleton entities (totally unique, or based on some properties). Sonata Admin friendly with automatic integration.
Install
Register the bundle to your 'app/AppKernel.php'
That's it!
Usage
Tell that your entity is a singleton
Just need to implement the Umanit\DoctrineSingletonBundle\Model\SingletonInterface
Now, if you try to create 2 entities of type "Page", you'll get a NonUniqueException
More complex unicity
If you want to implement a more complex unicity, for example, you want the entity to be unique per local, you can subscribe to the
FilterSingletonEvent::SINGLETON_FILTER_EVENT
event to modify the filters
(filters are used in a findBy()
clause)
e.g (from TranslationBundle)
services.yml
Get singleton
In order to get your singleton instances, you can use the provided helpers for your PHP code or twig :
The method getSingleton($className, array $filters = [], $instantiateIfNotFound = false)
can take from 1 to 3 arguments :
$className
: FQCN of the class to get$filters
: Filters to apply to get the singleton (e.g. :getSingleton("MyClass", ["locale" => "en"])
)$instantiateIfNotFound
: If the entity is not found, returns an empty entity instead ofnull
Integrating into SonataAdmin
The bundle will automatically remove add
and list
button from the list and the edit view.
If you use the UmanitDoctrineSingletonBundle:Sonata\SingletonCRUD
as a controller for your admin class, you'll have a redirection
to create
if there's no entity found, to edit
if there's only one, and to list
if you have more than one.
e.g :
All versions of doctrine-singleton-bundle with dependencies
symfony/framework-bundle Version >=5.0
doctrine/doctrine-bundle Version ^2.7
doctrine/orm Version ^2.13