Download the PHP package dealnews/db without Composer
On this page you can find all versions of the php package dealnews/db. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package db
Short Description Database Library providing a PDO factory and CRUD operations
License BSD-3-Clause
Informations about the package db
DealNews Datbase Library
Factory
The factory creates PDO objects using DealNews\GetConfig to
read database settings (from [app home]/etc/config.ini) and create a PDO database connection.
Supported Settings
| Setting | Description |
|---|---|
| type | This may be one of pdo, mysql, or pgsql. All types return PDO connections. |
| dsn | A valid PDO DSN. See each driver for specifics |
| db | The name of the database. For PDO, this is usually in the DSN. |
| server | One of more comma separated servers names. Not used by the pdo type. |
| port | Server port. Not used by the pdo type. |
| user | Database user name. Not all PDO drivers require one. |
| pass | Database password. Not all PDO drivers require one. |
| charset | Character set to use for mysql connections. The default is utf8mb4. |
| options | A JSON encoded array of options to pass to the PDO constructor. These vary by driver. |
Example config.ini file
This file should be stored in a directory named etc in the application's home directory.
Usage
Example:
CRUD
The CRUD class is a helper that wraps up common PDO logic for CRUD operations.
Basic Usage
Advanced Usage
The class also exposes a run method which is used internally by the other
methods. Complex queries can be run using this method by providing an SQL
query and a parameter array which will be mapped to the prepared query. It
returns a PDOStatement object.
Data Mapper Pattern
This library includes an abstract mapper class for creating data mapper classes. The data mapper pattern separates the object from how it is stored. A value object is created and a data mapper that is responsible for CRUD operations to and from a datastore.
To load, save, delete, etc. the mapper is used like so.
Generating Value Objects and Mappers
This library includes a script for generating value objects and mappers. It has been tested and
works with MySQL and PostgreSQL. Once installed in your project, the script can be run from the
vendor/bin directory.
Base Class for Value Objects
This library will work with plan PHP classes that do not extend any other class. However, using a base class such as Moonspot\ValueObjects can make working with the value objects easier.
Nested Mappers, Relational Data, and More
Documentation coming
Query Builder
The Query class provides a fluent interface for building complex SELECT queries
with JOINs, subqueries, GROUP BY, HAVING, and more. It complements CRUD for queries
that go beyond simple single-table operations.
Basic Usage
Complex Queries with JOINs
Nested WHERE Conditions
Available Methods
| Method | Description |
|---|---|
select(array $columns) |
Set SELECT columns |
from(string $table, ?string $alias) |
Set FROM table |
join(), innerJoin(), leftJoin(), rightJoin() |
Add JOIN clauses |
where(), orWhere() |
Add WHERE conditions |
whereIn(), whereNotIn() |
Add IN conditions |
whereNull(), whereNotNull() |
Add NULL checks |
whereRaw(string $sql, array $params) |
Add raw WHERE SQL |
groupBy(array $columns) |
Add GROUP BY |
having(), orHaving() |
Add HAVING conditions |
orderBy(string $column, string $direction) |
Add ORDER BY |
limit(int $limit) |
Set LIMIT |
offset(int $offset) |
Set OFFSET |
getSql() |
Build and return the SQL string |
getParams() |
Get the bound parameters |
Query::raw(string $value, array $params) |
Create a raw SQL fragment |
Testing
By default, only unit tests are run. To run the functional tests the host
machine will need to be a docker host. Also, the pdo_pgsql, pdo_mysql, and
pdo_sqlite extensions must be installed on the host machine. PHPUnit will
start and stop docker containers to test the MySQL and Postgres connections.
Use --group functional when running PHPUnit to run these tests.
All versions of db with dependencies
ext-pdo Version *
dealnews/console Version ^1.0.1
dealnews/data-mapper Version ^3.4
dealnews/get-config Version ^2.2