Simple relational database layer that abstracts SQL script to more usual persistence functions. It still provides a selectSQL function for more advanced and customized queries.
PHP: >=7.0
$ composer require tdw/rdb
<?php
$config = [
'db_driver' => 'mysql',
'db_host' => 'localhost',
'db_port' => '3306',
'db_name' => 'blog',
'db_user' => 'root',
'db_pass' => 'root'
];
$rdb = new \Tdw\RDB\Wrapper($config);
$database = $rdb->getDatabase();
<?php
try {
//select
$select = $database->select('posts');
/**@var \Tdw\RDB\Result\Select $result*/
$result = $select->execute();
print_r($result->fetchAll());
} catch (Tdw\RDB\Exception\StatementExecuteException $e) {
die($e->getPrevious());
}
<?php
try {
//insert
$insert = $database->insert(
'posts',
['title'=>'Post title','description'=>'Post desc']
);
/**@var \Tdw\RDB\Result\Insert $result*/
$result = $insert->execute();
print_r($result->lastInsertId());
} catch (Tdw\RDB\Exception\StatementExecuteException $e) {
die($e->getPrevious());
}
<?php
try {
//update
$update = $database->update(
'posts',
['title'=>'Post title','description'=>'Post desc'],
['id' => 1]
);
/**@var \Tdw\RDB\Result\Update $result*/
$result = $update->execute();
print_r($result->rowCount());
} catch (Tdw\RDB\Exception\StatementExecuteException $e) {
die($e->getPrevious());
}
<?php
try {
//delete
$delete = $database->delete('posts', ['id' => 1]);
/**@var \Tdw\RDB\Result\Delete $result*/
$result = $delete->execute();
print_r($result->rowCount());
} catch (Tdw\RDB\Exception\StatementExecuteException $e) {
die($e->getPrevious());
}
->select(string $table, array $columns = ['*'])
->insert(string $table, array $parameters)
->update(string $table, array $parameters, array $conditions)
->delete(string $table, array $conditions)
->selectSQL(string $sql, array $parameters = [])
->beginTransaction()
->commit()
->rollBack()
Tdw\RDB\Statement\Select
Tdw\RDB\Statement\Insert
Tdw\RDB\Statement\Update
Tdw\RDB\Statement\Delete
Used only in Tdw\RDB\Statement\Select
->columns(array $columns)
->join(
string $childTable,
string $foreignKeyChild,
string $operator,
string $primaryKeyParent
)
->where(string $column, string $operator, $value)
->orWhere(string $column, string $operator, $value)
->between(string $column, $valueOne, $valueTwo)
->notBetween(string $column, $valueOne, $valueTwo)
->orBetween(string $column, $valueOne, $valueTwo)
->orNotBetween(string $column, $valueOne, $valueTwo)
->in(string $column, array $subSet)
->notIn(string $column, array $subSet)
->orIn(string $column, array $subSet)
->orNotIn(string $column, array $subSet)
->like(string $column, string $value)
->orLike(string $column, string $value)
->notLike(string $column, string $value)
->orNotLike(string $column, string $value)
->null(string $column)
->orNull(string $column)
->notNull(string $column)
->orNotNull(string $column)
->orderBy(string $columns, $designator = 'ASC')
->limit(int $count, int $offset = 0)
Used all statement
->execute(): //respective result
->parameters(): array
Tdw\RDB\Result\Select
Tdw\RDB\Result\Insert
Tdw\RDB\Result\Update
Tdw\RDB\Result\Delete
Used only in Tdw\Result\Select
->fetchAll(): \Tdw\RDB\Contract\Collection
->fetch(): \Tdw\RDB\Contract\Item
Used only in Tdw\Result\Insert
->lastInsertId(string $name = null)
Used all result
->rowCount()
->get($key);
->all(): array;
->isEmpty(): bool;
->contains($value): bool;
->keys(): array;
->values(): array;
->shift();
->pop();
->push($value);
->prepend($value);
->remove($key);
->search($value);
->sort(\Closure $callback);
->each(\Closure $callback);
->filter(\Closure $callback): Collection;
->map(\Closure $callback): Collection;
->add($key, $value): Collection;
->clear();
Add new features.
The Tdw RDB is open-sourced software licensed under the MIT license.