Download the PHP package vrok/ref-helper without Composer
On this page you can find all versions of the php package vrok/ref-helper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vrok/ref-helper
More information about vrok/ref-helper
Files in vrok/ref-helper
Package ref-helper
Short Description Utilities to support polymorphic associations with Doctrine2 entities
License MIT
Homepage https://www.vrok.de/
Informations about the package ref-helper
Reference Helper
Library to support polymorphic asssociations with Doctrine 2 in Zend Framework 3.
Many applications have polymorphic relations where we don't know (and don't want to know in advance) which entities may be assigned to which (e.g. which entity is owned by which). For example a bank account may be owned by an user or an organization, a validation may belong to a bank account or an user.
To support this loose coupling we store the class of the referenced entity and the identifiers forming the primary key (e.g. autoincrement or composite keys) in the referencing entity in separate columns. The referencing entities should be kept simple POPOs, they need to implement , which is implemented in for re-use. The interface and trait support multiple references on one entity, for example to reference a creator and an owner.
The builds on this principle by providing functions to set and retrieve the referenced entities. The helper also has the ability to restrict references to only one or some targeted classes.
Usage
Entity preparation
1) Implement the on your entity that should store the reference to another object, use for simplicity 2) Add one or more references to the entity by defining the property and , properties for each reference
Now and can store references to any other Doctrine entity.
Set & get references with the ReferenceHelper
Using the you can now set and fetch references:
Restrict reference target classes
To restrict the ReferenceHelper to allow only one or some classes as targets add something like this to your config:
Now for the reference only instances of (and child classes) are accepted. Every other reference on will still allow every target class. Entity classes not listed in accept every target for every reference.
Querying for entities
Because is a separate column from the identifiers we can easily filter for every object referencing an entity of class :
We can also use the to get the values to filter for usage in a queryBuilder etc:
The ReferenceHelper can not add the conditions to a given QueryBuilder for you because it would have to know the alias to use (e.g. if you are using joins) and wether to to combine with previous conditions using "AND" or "OR". Maybe you even want to search for multiple conditions at once:
All versions of ref-helper with dependencies
doctrine/doctrine-orm-module Version ^1.1.5
zendframework/zend-servicemanager Version ^3.3.2