Download the PHP package lumax/aurora-db without Composer
On this page you can find all versions of the php package lumax/aurora-db. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package aurora-db
Luma | Aurora DB
The small but mighty PHP database component.
- Installation
- Usage
- The
Aurora
Model- Create Database Tables
- Create
Aurora
Classes- Nullable Columns
- OneToMany Relationships
- Table & Schema
- CRUD Methods
- Pagination
- Query Builder
- The
Installation
Usage
In your applications entrypoint, connect to your database:
In its simplest form, we can use the above DatabaseConnection
instance to get the PDO
database connection:
This allows you to use Aurora
models to interact with your database via a single shared connection.
The Aurora
Model
Create Database Tables
An Aurora
model is an entity that is specifically designed to interact with a corresponding database table. One instance
of an Aurora
model relates to one row in its corresponding database table.
Take the following tables:
Create Aurora
Classes
We can create the following Aurora
models which will allow us to create, retrieve, update and delete records within these tables.
All Aurora
models have a getId()
method which returns the value of the property with the #[Identifier]
attribute.
Identifiers must be protected
and are required as part of a valid Aurora
model. All other properties which you
also wish to map to a database column should use the #[Column($name)]
attribute.
Nullable Columns
If your database table contains any nullable columns it is important to ensure that the associated property has been type hinted as nullable:
OneToMany Relationships
For handling one-to-many relationships, for example fetching all Article
models created by a User
, we can add a property
to hold our articles (on the User
model):
It's as simple as that, now calling getArticles()
from a User
instance will return a Collection
containing all the
articles written by that user.
Table & Schema
By default, the table name associated with your Aurora
class will be the same name as your class - so User
and Article
in this case.
In addition, no schema will be set for your class - output queries will look something like this SELECT * FROM User ...
.
You may wish to specify a schema and table name:
This would create/run queries that look something like this SELECT * FROM Core.User ...
.
CRUD Methods
Let's add some new records to the tables we created:
And we can retrieve them:
The save()
method can also be used to update existing records:
And we can also delete existing records:
Pagination
Aurora also allows you to paginate your data:
Query Builder
The Aurora
model includes a handy query builder. Some important notes:
In order to execute any query builder statement and retrieve the results, you must call the
get
method.All query builder results are returned with the specified columns as well as their primary identifier, so this does not need to be specified.
There are 3 possible return types following a get() call:
- When one result is returned, this returns as an instance of the calling class.
- When more than one result is returned, this returns an array containing instances of the calling class.
- When zero results are returned, NULL is returned by the query builder.
Here are some of the methods that you can use: