Download the PHP package clancats/hydrahon without Composer
On this page you can find all versions of the php package clancats/hydrahon. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package hydrahon
Hydrahon
Hydrahon is a standalone database / SQL query builder written in PHP. It was built to enhance existing frameworks, libraries and applications that handle the database connection on their own. It does not come with a PDO or mysqli wrapper. The naming is heavily inspired by Eloquent and the Kohana Framework Database component.
What does that mean "Standalone query builder"?
Hydrahon only generates a query string and an array of parameters. On its own, it is not able to execute a query.
Status
- The Hydrahon MySQL query builder is stable and used in production.
- The Hydrahon AQL (Arango Query Language) query builder is currently in development.
- A builder for Elasticsearch is on my mind but not in development.
Installation
Hydrahon follows PSR-4
autoloading and can be installed using composer:
Documentation 💡
The full documentation can be found on clancats.io
Quick Start (MySQL) ⚡️
Hydrahon is designed to be a pretty generic query builder. So for this quick start, we stick with SQL.
Create a builder
Again this library is not built as a full database abstraction or ORM, it is only and will always be only a query builder. This means we need to implement the database connection and fetching by ourselves. The Hydrahon constructor therefore requires you to provide a callback function that does this, and returns the results.
In this example, we are going to use PDO
And we are ready and set. The variable $h
contains now a MySQL query builder.
Setup a simple table
To continue with our examples, we need to create a simple MySQL table.
Inserting
Currently, we do not have any data, to fix this let's go and insert some.
Will execute the following query:
As you can see Hydrahon automatically escapes the parameters.
However, because we are humans that get confused when there are hundreds of thousands of questions marks, I will continue to always display the runnable query:
Updating
Ah snap, time runs so fast, "Ray" is actually already 26.
Generating:
Currently, you might think: "Well isn't it much simpler to just write the SQL query? I mean the PHP code is even longer...".
You have to understand that these are some very very basic examples the Hydrahon query builder starts to shine when things get more complex. However, a "Quick Start" is just the wrong place for complicated stuff, so throw an eye on the full documentation.
Deleting
Dammit John, I hate you...
Generating:
Selecting
And finally, fetch the data.
Generating:
Result:
Notice that we use ->get()
to actually fetch data, while we used ->execute()
for our previous queries (updates, inserts and deletes). See the full documentation for more information about the Hydrahon runners methods.
Where conditions
For the next few examples, lets assume a larger dataset so that the queries make sense.
Chaining where conditions:
Notice how omitting the operator in the first condition ->where('age', 21)
makes Hydrahon default to =
.
By default all where conditions are defined with the and
operator.
Different where operators:
Please check the relevant section in the full documentation for more where-functions, like
whereIn()
whereNotIn()
whereNull()
whereNotNull()
Where scopes
Allowing you to group conditions:
Joins
Joining tables:
Grouping
Grouping data:
Ordering
Ordering data:
Limiting data
Limit and offset:
Small reminder this is the quick start, check out the full docs.
Credits
License
The MIT License (MIT). Please see License File for more information.