Download the PHP package rds/hydrogen without Composer
On this page you can find all versions of the php package rds/hydrogen. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package hydrogen
Short Description More faster and convenient Doctrine ORM abstraction layer
License MIT
Informations about the package hydrogen
- Introduction
- Installation
- Server Requirements
- Installing Hydrogen
- Usage
- Retrieving Results
- Retrieving All Entities
- Retrieving A Single Entity
- Retrieving A List Of Field Values
- Aggregates And Scalar Results
- Selects
- Additional fields
- Where Clauses
- Simple Where Clauses
- Or Statements
- Additional Where Clauses
- Parameter Grouping
- Ordering
- Grouping
- Limit And Offset
- Embeddables
- Relations
- Joins
- Joins Subqueries
- Nested Relationships
- Query Scopes
- Collections
- Higher Order Messaging
- Destructuring
Introduction
Hydrogen provides a beautiful, convenient and simple implementation for working with Doctrine queries. It does not affect the existing code in any way and can be used even in pre-built production applications.
Installation
Server Requirements
The Hydrogen library has several system requirements. You need to make sure that your server meets the following requirements:
- PHP >= 7.1.3
- PDO PHP Extension
- Mbstring PHP Extension
- JSON PHP Extension
- doctrine/orm >= 2.5
- illuminate/support >= 5.5
Installing Hydrogen
Hydrogen utilizes Composer to manage its dependencies. So, before using Hydrogen, make sure you have Composer installed on your machine.
Stable
Dev
Usage
Hydrogen interacts with the repositories of the Doctrine. In order to take advantage of additional features - you need to add the main trait to an existing implementation of the repository.
After that you get full access to the query builder.
Retrieving Results
Retrieving All Entities
You may use the ->query()
method on the Repository to begin a query.
This method returns a fluent query builder instance for the given repository,
allowing you to chain more constraints onto the query and then finally
get the results using the ->get()
method:
The get()
method returns an array
containing the results,
where each result is an instance of the object (Entity) associated
with the specified repository:
In addition, you can use the method collect()
to
get a collection that is compatible with ArrayCollection:
Note: Direct access to the Hydrogen build, instead of the existing methods, which is provided by the Doctrine completely ignores all relations (like:
@OneToMany(..., fetch="EAGER")
).
Retrieving A Single Entity
If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single Entity object:
If you don't even need an entire row, you may extract a single
values from a record using additional arguments for ->first()
method.
This method will return the value of the column directly:
Retrieving A List Of Field Values
If you would like to retrieve an array or Collection containing the values of a single Entity's field value,
you may use the additional arguments for ->get()
or ->collect()
methods.
In this example, we'll retrieve a Collection of user ids and names:
Aggregates and Scalar Results
The query builder also provides a variety of aggregate methods such as count
, max
, min
,
avg
, and sum
. You may call any of these methods after constructing your query:
Of course, you may combine these methods with other clauses:
In the event that your database supports any other functions,
then you can use these methods directly using ->scalar()
method:
The first argument of the ->scalar()
method requires specifying the field that should be
contained in the result. The second optional argument allows you
to convert the type to the desired one.
Allowed Types
Type | Description |
---|---|
int |
Returns an integer value |
float |
Returns a float value |
string |
Returns a string value |
bool |
Returns boolean value |
callable |
Returns the Closure instance |
object |
Returns an object |
array |
Returns an array |
iterable |
array alias |
Query Invocations
Method | Description |
---|---|
get |
Returns an array of entities |
collect |
Returns a Collection of entities |
first |
Returns the first result |
scalar |
Returns the single scalar value |
count |
Returns count of given field |
sum |
Returns sum of given field |
avg |
Returns average of given field |
max |
Returns max value of given field |
min |
Returns min value of given field |
Selects
Using the select()
method, you can specify a
custom select clause for the query:
Also, this expression can be simplified and rewritten in this way:
Additional fields
Entity
You noticed that if we specify a select, then in the response we get the data
of the select, ignoring the Entity. In order to get any entity in the response,
we should use the method withEntity
:
Raw Columns
Sometimes some fields may not be contained in Entity, for example, relation keys. In this case, we have no choice but to choose this columns directly, bypassing the structure of the Entity:
Where Clauses
Simple Where Clauses
You may use the where method on a query builder instance to add where clauses to the query. The most basic call to where requires three arguments. The first argument is the name of the column. The second argument is an operator, which can be any of the database's supported operators. Finally, the third argument is the value to evaluate against the column.
For example, here is a query that verifies the value of the "votes" Entity field is equal to 100:
For convenience, if you want to verify that a column is equal to a given value, you may pass the value directly as the second argument to the where method:
Of course, you may use a variety of other operators when writing a where clause:
Or Statements
You may chain where constraints together as well as add or
clauses to the query. The orWhere
method accepts the same
arguments as the where method:
Alternatively, you can use the ->or
magic method:
Additional Where Clauses
whereBetween
The whereBetween
method verifies that a Entity fields's value is between two values:
whereNotBetween
The whereNotBetween
method verifies that a Entity field's value lies outside of two values:
whereIn / whereNotIn
The whereIn
method verifies that a given Entity field's value
is contained within the given array:
The whereNotIn
method verifies that the given Entity field's value
is not contained in the given array:
whereNull / whereNotNull
The whereNull
method verifies that the value of
the given Entity field is NULL
:
The whereNotNull
method verifies that
the Entity field's value is not NULL
:
like / notLike
The like
method verifies that the value of
the given Entity field like given value:
The notLike
method verifies that the value of
the given Entity field is not like given value:
Parameter Grouping
Sometimes you may need to create more advanced where clauses such as "where exists" clauses or nested parameter groupings. The Hydrogen query builder can handle these as well. To get started, let's look at an example of grouping constraints within parenthesis:
As you can see, passing a Closure
into the where
method
instructs the query builder to begin a constraint group.
The Closure
will receive a query builder instance which
you can use to set the constraints that should be contained
within the parenthesis group. The example above will
produce the following DQL:
In addition to this, instead of the where
or orWhere
method,
you can use another options. Methods or
and and
will do the same:
Ordering
orderBy
The orderBy
method allows you to sort the result of the query
by a given column. The first argument to the orderBy
method
should be the column you wish to sort by, while the second argument
controls the direction of the sort and may be either asc or desc:
Also, you may use shortcuts asc()
and desc()
to simplify the code:
latest / oldest
The latest and oldest methods allow you to easily order
results by date. By default, result will be ordered by the
createdAt
Entity field. Or, you may pass the column name
that you wish to sort by:
Grouping
groupBy
The groupBy
method may be used to group the query results:
You may pass multiple arguments to the groupBy
method to group by
multiple columns:
having
The having
method's signature is similar to that
of the where
method:
Limit And Offset
skip / take
To limit the number of results returned from the query, or
to skip a given number of results in the query, you may
use the skip()
and take()
methods:
Alternatively, you may use the limit
and offset
methods:
before / after
Usually during a heavy load on the DB, the offset
can shift while
inserting new records into the table. In this case it is worth using
the methods of before()
and after()
to ensure that the subsequent
sample will be strictly following the previous one.
Let's give an example of obtaining 10 articles, which are located after the id 15:
range
You may use the range()
method to specify exactly which
record you want to receive as a result:
Embeddables
Embeddables are classes which are not entities themselves, but are embedded in entities and can also be queried by Hydrogen. You'll mostly want to use them to reduce duplication or separating concerns. Value objects such as date range or address are the primary use case for this feature.
To manage Embeddables through queries, you can use the point (.
) operator:
Relations
The Doctrine ORM provides several types of different relations: @OneToOne
,
@OneToMany
, @ManyToOne
and @ManyToMany
. And "greed" for loading these
relations is set at the metadata level of the entities. The Doctrine
does not provide the ability to manage relations and load them
during querying, so when you retrieve the data, you can encounter
N+1
queries without the use of DQL, especially
on @OneToOne
relations, where there is simply no other loading option.
The Hydrogen allows you to flexibly manage how to obtain relations at the query level, as well as their number and additional aggregate functions applicable to these relationships:
If you create a basic query to the repository, in this case you will
get the same N+1
, where for each element the blocks will be
generated with one additional query, for each related entity.
Joins
One of the options for working with relations in the Doctrine are joins.
In order for the query to ask for data with a relation in one query, you
should use join
(uses INNER JOIN
) or leftJoin
(uses LEFT JOIN
) methods:
Please note that when using joins, you can not use
limit
, because it affects the total amount of data in the response (i.e., including relations), rather than the number of parent entities.
Joins Subqueries
We can also work with additional operations on dependent entities. For example, we want to get a list of users (customers) who have more than 100 rubles on their balance sheet:
Note: Operations using
join
affect the underlying query.
Nested Relationships
So, if we need all the customers that have been ordered, for example, movie tickets, we need to make a simple request:
Query Scopes
Sometimes it takes a long time to build a whole query, and some parts of it already repeat existing ones. In this case, we can use the mechanism of scopes, which allows you to add a set of methods to the query, which in turn must return parts of the query we need:
Collections
As the base kernel used a Illuminate Collections but some new features have been added:
- Add HOM proxy autocomplete.
- Added support for global function calls using the Higher Order Messaging and the Pattern Matching.
Higher Order Messaging
Pattern "_
" is used to specify the location of the delegate in
the function arguments in the higher-order messaging while using global functions.
Destructuring
Beethoven approves.