colorium / orm
Simple ORM Library
v0.2
2016-01-05 08:35 UTC
Requires
- php: >=5.6
- colorium/runtime: >=0.1
This package is not auto-updated.
Last update: 2024-11-05 19:10:04 UTC
README
Basic usage
use Colorium\Orm\MySQL; $mysql = new MySQL('dbname');
You can also map some model classes that will be use intead of \stdClass
:
use Colorium\Orm\MySQL; $mysql = new MySQL('dbname', [ 'user' => My\App\User::class ]);
Read
Read many, return a set of \stdClass
(or specified class if previously mapped)
$users = $mysql->user->fetch(); $users = $mysql->user->where(...)->sort(...)->limit(...)->fetch();
Where clause (working operators: >
, >=
, <
, <=
, is
, not
, exists
, in
with array as parameter, =
as default)
$users = $mysql->user->where('age', 27)->fetch(); $users = $mysql->user->where('age >', 27)->fetch(); $users = $mysql->user->where('age', [25, 26, 27]])->fetch(); // or multiple where $users = $mysql->user->where(['age' => 27, 'eyes' => 'green'])->fetch();
Sort clause
$users = $mysql->user->sort('city')->fetch(); // ASC $users = $mysql->user->sort('city', SORT_DESC)->fetch();
Limit clause
$users = $mysql->user->limit(5)->fetch(); // 5 records from start $users = $mysql->user->limit(5, 10)->fetch(); // 10 records from the 5th
Read one, return a \stdClass
(or specified class if previously mapped)
$user = $mysql->user->one(); // first record $user = $mysql->user->where(...)->one();
Write
Add record (you can pass an array or an object)
$id = $mysql->user->add([ 'username' => 'New player' ]);
Edit record
$user = $mysql->user->where('id' => 15)->one(); $user->username = 'Old player'; $mysql->user->where('id', 15)->edit($user);
Drop record
$mysql->user->where('id' => 15)->drop();
Custom SQL query
$users = $mysql->raw('select * from `user`');
Hub
Hub is a global container for orm sources, used by model helper :
Use Colorium\Orm\Hub; Hub::source($mysql); $users = Hub::user()->fetch();
Model
Model is more convenient way of using Colorium Orm (do not forget to setup Hub
before) :
class User { use Colorium\Orm\Model; /** @var int */ public $id; /** @var string */ public $username; /** @var string */ public $password; }
Then :
$user = User::one(['id' => 15]); $user->username = 'Winner'; $user->save();
Install
composer require colorium/orm