Download the PHP package anax/database without Composer
On this page you can find all versions of the php package anax/database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download anax/database
More information about anax/database
Files in anax/database
Package database
Short Description Anax Database module, to work with databases.
License MIT
Homepage https://dbwebb.se/anax
Informations about the package database
Anax Database
Anax Database module as a database abstraction layer (DBA) for wrapping PHP PDO with an additional layer of utilities and ease of use together with ability to use configuration file and attach into an Anax installation as a servide in $di.
The module is tested using MySQL and SQLite.
There are separate modules for a Database Query Builder in anax\database-query-builder
and a Database Active Record in anax\database-active-record
.
Table of content
- Install
- Development
- Class, interface, trait
- Exceptions
- Configuration file
- DI service
- Access as framework service
- Create a connection
- Perform a SELECT query
- Fetch versus FetchAll
- FetchClass
- FetchInto
- Perform an INSERT, UPDATE, DELETE query
- Last insert id
- Row count, affected rows
- Throw exception on failure
- Dependency
- License
Install
You can install the module from anax/database
on Packagist using composer.
You can then copy the default configuration files as a start.
Development
To work as a developer you clone the repo and install the local environment through make. Then you can run the unit tests.
Class, interface, trait
The following classes exists.
Class, interface, trait | Description |
---|---|
Anax\Database\Database |
Wrapper class for PHP PDO with enhanced error handling and extra utilities. |
Anax\Database\DatabaseDebug |
An alternative class that can be used for debugging database related issues. |
Exceptions
All exceptions are in the namespace Anax\Database\Exception\
. The following exceptions exists and may be thrown.
Exception | Description |
---|---|
Exception |
General module specific exception, for example when connection fail. |
Configuration file
This is a sample configuration file. It is usually stored in config/database.php
when used together with Anax.
You can use if-statements within the configuration file to serve different configurations for local development environment, staging and or production environment.
DI service
The database is created as a framework service within $di
. The following is a sample on how the database service is created through config/di/db.php
.
The setup callback works like this.
- The database object is created.
- The configuration file is read.
- The configuration is applied.
Access as framework service
You can access the database module as a framework service.
Create a connection
You must connect to the database before using it.
You may call $db->connect()
many times, the connection is however only made once, the first time, so it is safe to call the method several times.
Perform a SELECT query
You connect and perform the query which returns a resultset.
The contents of $res
is depending on the configuration key which default is set to "fetch_mode" => \PDO::FETCH_OBJ,
.
You can also separate executeFetchAll()
into two separate commands execute()
and fetchAll()
.
Fetch versus FetchAll
With fetchAll()
you fetch all matching rows in an array. When no matching rows are found you get en ampty array.
With fetch()
you get the first item in the resultset. You may use this when your resultset only contain one row.
The content of $res
is now an object of type \StdClass
and you access the resultset columns by their name, for example $res->id
.
FetchClass
You can fetch the resultset into an object instantiated from a specified class.
The resultset is inserted into a new object of the class "\Anax\SomeClass"
.
There is also executeFetchAllClass()
which fetches an array of all matching rows as new objects of the class.
FetchInto
You can fetch the resultset into an existing object as public properties.
The resultset is inserted into the object $obj
.
Perform an INSERT, UPDATE, DELETE query
These queries, that updates the database, uses $db->execute()
and does not return a resultset.
Last insert id
You can check the last inserted id when doing INSERT where the primary key is auto generated.
Row count, affected rows
You can check how many rows that are affected by the last INSERT, UPDATE, DELETE statement.
Throw exception on failure
Exception are in general thrown as soon as something fails.
The exception is module specific Anax\Database\Exception\Exception
and contains details from the error message from the PDO layer, either from the statement or from the PDO-object, depending on what type of error happens.
Dependency
No particular dependencies. The module is usually used within an Anax installation but can also be used without Anax.
License
This software carries a MIT license. See LICENSE.txt for details.