Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Advanced Yii ORM Plugin

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 behaviors
Octocat-spinner-32 CEntity.php
Octocat-spinner-32 CUController.php
Octocat-spinner-32 DbIDGenerator.php
Octocat-spinner-32 README
Octocat-spinner-32 UnitOfWork.php
README
YORM

Advanced Yii ORM Plugin

YORM是一款Yii的第三方ORM插件,为Yii提供一系列企业应用架构模式中的高级特性,如:读写分离,分表,分库,领域实体,缓存,全局唯一ID以及工作单元。

YORM主要通过继承了Yii的CActiveRecord类开发的CEntity类(实体类),以及一些实现高级特性的行为类,来使Yii程序支持复杂的系统架构设计。


Entity 实体类
class User extends CEntity
{

    // 实现读写分离(分库)
    public function getDbConnection()
    {
        if($this->isDbReading())
        {
            return Yii::app()->readDb;
        }
        else
        {
            return Yii::app()->writeDb;
        }
    }

    // 实现分表, 根据Id进行散列
    private $tableName = "tbl_user_1";
    public function updateMeta($id)
    {
        $this->tableName = "tbl_user_".($id%10);
            
        $this->refreshMetaData();
        
        return $this;
    }

    public function tableName()
    {
        return $this->tableName;
    }

    public function findByPk($pk)
    {
        $this->updateMeta($pk);
        return parent::findByPk($pk);
    }

    // 增加工作单元特性
    public function behaviors() {
        return array('UnitOfWorkBehavior' => array('class' => 'UnitOfWorkBehavior'));
    }

}

使用:

// 分表
$user = User::model()->updateMeta(10)->findByPk(10);
Something went wrong with that request. Please try again.