Giter Site home page Giter Site logo

yorm's Introduction

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

yorm's People

Contributors

lifei avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.