nochso/orm

注重关系和性能的ORM。

1.3.5 2015-11-04 19:45 UTC

This package is not auto-updated.

Last update: 2024-09-11 15:39:11 UTC


README

License GitHub tag Build Status SensioLabs Insight Coverage Status Dependency Status

稳定的ActiveRecord实现

  • 流畅的查询构建器
  • 与MySQL和SQLite进行了测试
  • 灵感来源于 Paris,但减少了魔法以提高自动补全

使用了以下约定

  • 每个表都需要一个继承自 nochso\ORM\Model 的类。
  • 默认情况下,类名通过下划线转换为表名。
    • 否则,您可以通过覆盖 protected static $_tableName 来实现
  • 模型类的公共属性对应于列名。

从表 blog_post 中选择所有标题匹配 "Hello %" 的行,按 creation_date 排序。然后一次性更新所有标题。

$posts = BlogPost::select()
    ->like('title', 'Hello %')
    ->orderAsc('creation_date')
    ->all();
foreach ($posts as $primaryKey => $post) {
    $post->title .= ' and goodbye';
}
$posts->save();

安装

获取composer 并要求 nochso/orm

composer require nochso/orm

示例

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));

变更日志

查看 CHANGELOG 以获取发布之间的完整更改历史。