Foundry is a simple yet powerful SQL query builder written in PHP.
- Create a db connection
- Build a SELECT query
- Build an INSERT query
- Build a DELETE query
- Build an UPDATE query
- Execute the queries
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);