https://github.com/nochso/orm2
https://github.com/nochso/orm2
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/nochso/orm2
- Owner: nochso
- License: mit
- Created: 2014-12-06T10:55:29.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-02-21T10:02:28.000Z (over 10 years ago)
- Last Synced: 2025-08-04T22:03:37.144Z (11 months ago)
- Language: PHP
- Size: 111 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# nochso/orm
[](https://packagist.org/packages/nochso/orm)
[](https://github.com/nochso/ORM2/releases)
[](https://travis-ci.org/nochso/ORM2)
[](https://insight.sensiolabs.com/projects/c4694967-5a09-400f-b493-728935812c7a)
[](https://coveralls.io/github/nochso/ORM2?branch=master)
[](https://www.versioneye.com/user/projects/558dc123316338001e00001a)
A stable ActiveRecord implementation:
- fluent query builder
- tested with MySQL and SQLite
- inspired by [Paris](http://j4mie.github.io/idiormandparis/) 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`
- 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.
```php
$posts = BlogPost::select()
->like('title', 'Hello %')
->orderAsc('creation_date')
->all();
foreach ($posts as $primaryKey => $post) {
$post->title .= ' and goodbye';
}
$posts->save();
```
## Installation
[Get composer](https://getcomposer.org) and require `nochso/orm`.
```
composer require nochso/orm
```
## Example
```php
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;
}
```
```php
// 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](CHANGELOG.md) for the full history of changes between releases.