Download the PHP package alex-unruh/repository without Composer
On this page you can find all versions of the php package alex-unruh/repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alex-unruh/repository
More information about alex-unruh/repository
Files in alex-unruh/repository
Package repository
Short Description An abstraction layer built on Doctrine DBAL Query Builder
License MIT
Homepage https://github.com/alex-unruh/repository
Informations about the package repository
Alex Unruh - Repository
This is an abstraction layer that extends Doctrine DBAL Query Builder with methods to help and reduce the amount of code for bind values, for example.
Installation:
Usage:
Let's see below a scenario with an insert query using only the Doctrine DBAL QueryBuilder:
Now let's see the same query with a bit of abstraction using Repository::class
Behind the scenes, the repository class executes exactly the same procedure, making the binds safely and returning the same result. If you has a big table, this can be very useful. In fact, all the methods present in Doctrine DBAL Query Builder be present in Repository::class because, as we already said, it extends DBAL QB. You're free to use as you want.
Methods that don't exist in the parent class (Doctrine DBAL QueryBuilder)
setConnection( array $connection_params ): Repository
If you need to use the same coonection in multiple queries, like a transaction, this is very useful:
get()
Used in final of a select statement to return a multidimensional array
getFirst()
Used in final of a select statement to return only the first result of a query
addValues( array $array_data )
- @param array $array_data = An array containing pairs of key => value to be inserted in the table
setValues( array $array_data )
- @param array $array_data = An array containing pairs of key => value to be updated in the table
execute()
The execute method is responsible to make the safely bind params in insert, update and delete statements and return as the result the number of rows affected. It calls the setParameters() and executeStatement() methods by Doctrine DBAL parent class.
Although it is not visible, there will be a bindParam between the setValues and execute methods
setTypes( array $types ) : Repository
- @param array $types = An artray with pairs of key => values to be used in queries that you need to specify the type of the data to be inserted or updated. See more in Doctrine DBAL Docs
In some cases, when you work with Doctrine DBAL or other Query Builders, is necessary to set a data type of a specific column data. If you are store a cripted data in a table, for example, maybe was necessary to tell to Query Builder whats the type of this data because each type of database stores this differently.
In other cases, for security, you need to set the type of a value before stores him in your table, in setParameter method. If the type is different from the defined, an Exception will be thrown.
Lets see an example with Doctrine DBAL QueryBuilder:
Now, let's see with Repository::class
Extending the Repository::class:
Most queries can be performed using methods from the parent Repository class. But in some cases you may have more complex queries (like queries containing subqueries), which would "bloat" your controllers and you would like to put these queries in a separate class, using a repository pattern. For that you can create your own class and extend the repository parent class.
All classes that extends the Repositor::class needs to have at least the protected $table_name property to use the special crud methods that will be described below.
Another parameter that can be implemented in an inheritor class is $data_types which contains the data types described in the setTypes method above.
Don't waste time trying to understand the method below. While it works, it's just here to demonstrate a Repository pattern use case.
Methods available to be used only in extended classes: read, create, modify and remove.
All the methods described bellow neede to be used in clases that extends the Repositor::class because they use parameters defined in this classes, as $table_name or $data_types, for example.
read( array $data, string $table_alias = null ): Repository
- @param array $array_data = An array containing the data to be selected from the table
- @param string $table_alias = A table alias to be used in join statements (optional)
create(array $data): Repository
- @param array $array_data = An array containing pairs of key => value to be inserted in the table
Behind the scenes, repository::class wiil make the safely bind values using the $data_types array to each column if it be present on class properties.
modify( array $data, string $table_alias = null ): Repository
- @param array $array_data = An array containing pairs of key => value to be updated in the table
- @param string $table_alias = A table alias to be used in join statements (optional)
Behind the scenes, repository::class wiil make the safely bind values using the $data_types array to each column if it be present on class properties. Always use with where clauses
destroy( string $table_alias = null ): Repository
- @param string $table_alias = A table alias to be used in join statements (optional)
addParameter(string $key, string $val, string $type = null): Repository
- @param string $key = Parameter key
- @param string $val = Parameter value
- @param string $type = Parameter type
This method is recomended to be used when you use the method modify and you need to add new parameters to bind in other clauses (like where clauses).
Enjoy.