guvra/foundry

A simple yet powerful SQL query builder written in PHP.

1.0.2 2018-11-24 23:06 UTC

This package is auto-updated.

Last update: 2024-12-18 07:17:11 UTC


README

Description

Foundry is a simple yet powerful SQL query builder written in PHP.

Documentation

Usage Example

Initialization:

use Foundry\Connection;

$connection = new Connection(['dsn' => 'sqlite:db.sqlite']);

Select:

use Foundry\Parameter;

$select = $connection
    ->select()
    ->from(['t' => 'transactions'])
    ->join(['a' => 'accounts'], 'a.account_id = t.account_id')
    ->where('a.name', 'like', new Parameter('name'))
    ->orWhere('a.balance', 'between', [0, 1000])
    ->order('t.date desc');

$statement = $connection->query($select, [':name' => '%stock%']);
$rows = $statement->fetchAll();

Insert:

$query = $connection
    ->insert()
    ->ignore()
    ->into('accounts')
    ->columns(['name', 'balance'])
    ->values([['Account 1', 0], ['Account 2', 450.59]]);

$connection->query($query);

Update:

$query = $connection
    ->update()
    ->table('accounts')
    ->values(['name' => 'Account 5'])
    ->where('name', '=', 'Account 1');

$connection->query($query);

Delete:

$query = $connection
    ->delete()
    ->from('accounts')
    ->where('name', '=', 'Account 1');

$connection->query($query);