Download the PHP package digipolisgent/doctrine-extra without Composer
On this page you can find all versions of the php package digipolisgent/doctrine-extra. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download digipolisgent/doctrine-extra
More information about digipolisgent/doctrine-extra
Files in digipolisgent/doctrine-extra
Informations about the package doctrine-extra
Doctrine Extra's
1. Example's
In this package a BaseManager is included that uses the Assert en Resolvable trait. If we use this Manager we can do the following code examples ;
Select all new & processing orders paginated
Select all 'new' & 'processing' orders as an iterator
Count all 'new' & 'processing' orders
Add an extra parameter to an existing query
More example's are available below in the docs.
2. Filters
Create Filter objects to easily filter your repository. This package contains two default Filter objects to use ; 'AggregateFilter' and 'PropertyFilter'. Both are multipurpose but is is recommended to extend the AbstractFilter and create your own.
The filtering itself can be done by any class that uses the Resolver trait, and uses the Doctrine QueryBuilder object as a starting point.
Lets take the following example ;
We have an OrderRepository that implements the Resolver trait, and has a method "filter" that accepts an array of Doctrine Filters or one DoctrineFilter.
Using the PropertyFilter
The property filter can be used to filter on any entity's property. Lets Imagine an order has a "category" property, we want to filter on.
By now we have filtered orders for a certain category. We can also pass multiple filters as an array into the filter() method as an argument, they will all be combined in an andX expression.
Using the AggregateFilter
In case we want to use multiple filters in an orX expression, we can use the AggregateFilter. In the following example we will filter all 'new' or 'finished' orders;
We can combine multiple AggregateFilters to make a more complex expression. In the following example we will filter all 'new' or 'finished' orders that have 'multimedia' or 'books' as a category
Creating a custom DoctrineFilter
Simple property filter
We can also implement CategoryFilter if more entities have category as a property and we want to use it often.
If we want to create our own CategoryFilter, which is the most basic form of filtering by filtering on a single direct property we can do it this way ;
We only need to implement the CreateExpression() method, and create a simple expression that will be used in the Where clause in the QueryBuilder. Now this filter can be usd on all entities with the "category" property.
Sometimes we will need a more complex filter. Assume our Order entity has a relationship with User, and User has a username. We want to filter all orders by a user's username. We can create the following filter ;
More advanced example
This Filter is a bit more complex. Since we are not filtering a direct property of Order, but instead filter on a property of User which has a association with our Order object, we need to implement two more methods.
The function getAlias() will return a string, the alias defines the object's alias that has the property we are filtering on. In this case its 'user' because we will filter on user.email.
The AddAlias() method will be triggered by the Resolver when it checks if the 'user' alias is present in the QueryBuilder, an alias is present when it is OR selected OR joined. In this case we Left join our User object to our 'root' object, in this example 'Order'.
Now we have finished our UserEmailFilter, we can use this filter on any object that has User as a relation. We can for example have a Reservation entity and a Ticket entity that both have an association with User.
So by now we can do the following ;
3. Assert Results
This package includes the Assertable trait, which allows any class to pass a QueryBuilder object and retrieve different results from that same QueryBuilder object.
For example, we have a repository that uses the Assertable trait, we the following code ;
Because we use the assertable trait, and we pass our QueryBuilder object through the assertResult() method, we can manipulate what kind of result this function will return. By default this function will act as expected and return an array with all Order entities by a certain category.
If we want to retrieve the orders paginated, we can simply call the builder() method before calling getOrdersByCategory and the Assert trait will assert that the result passed as a result will be a Paginator object;
To see all possible outcomes of the assertResult() method, take the following code ;
The above code is located in the Assert trait.