nochso / orm
ORM with relations and performance in mind.
1.3.5
2015-11-04 19:45 UTC
Requires (Dev)
- phpunit/phpunit: 4.8.*
README
A stable ActiveRecord implementation:
- fluent query builder
- tested with MySQL and SQLite
- inspired by Paris but with less magic for better autocompletion
The following conventions are used:
- Every table requires a class inheriting from
nochso\ORM\Model
. - Class names are snaked_cased to table names by default.
- Otherwise you can override
protected static $_tableName
- Otherwise you can override
- Public properties of model classes correspond to column names.
Select all rows from table blog_post
with title matching "Hello %" ordered by creation_date
. Then update all titles at once.
$posts = BlogPost::select() ->like('title', 'Hello %') ->orderAsc('creation_date') ->all(); foreach ($posts as $primaryKey => $post) { $post->title .= ' and goodbye'; } $posts->save();
Installation
Get composer and require nochso/orm
.
composer require nochso/orm
Example
use nochso\ORM\Model; use nochso\ORM\Relation; class User extends Model { /* Actual database table name */ protected static $_tableName = 'user'; /* The Subscription class must have a field "user_id" to identify the user's subscriptions */ protected static $_relations = array( 'subscriptions' => array(Relation::HAS_MANY, '\TV\Model\Subscription') ); public $id; public $name; public $password; public $token; /* Lets you access the relation to the user's subscriptions. * Names must match with the key in $_relations */ public $subscriptions; }
// Fetch a user by his name $john = User::select()->eq('name', 'john doe')->one(); // or achieve the same using the primary key $sameJohn = User::select()->one($john->id); echo $john->name; // 'john doe' // Change and save his name $john->name = 'herbert'; $john->save(); // Loads the related list of \TV\Model\Subscription instances as defined in User::$_relations['subscriptions'] $john->subscriptions->fetch(); if (count($john->subscriptions) > 0) { $john->subscriptions[0]->delete(); } // Update certain columns of certain users User::select() ->in('user_id', array(3, 6, 15)) ->update(array('banned' => 1));
Change log
See the CHANGELOG for the full history of changes between releases.