duancuan7057 2013-05-06 13:13
浏览 91
已采纳

Yii多个表的一个模型

I have Yii application and two tables with same structure tbl and tbl_history:

Now want to create model so it will select table by parameter I send when calling model. For example:

MyModel::model('tbl')->find();
//and
MyModel::model('tbl_history')->find();

Find related article with solution in Yii forum. Made same changes and finally got this in MyModel:

private $tableName = 'tbl'; // <=default value
private static $_models=array();
private $_md;

public static function model($tableName = false, $className=__CLASS__)
{
    if($tableName === null) $className=null; // this string will save internal CActiveRecord functionality
    if(!$tableName)
        return parent::model($className);

    if(isset(self::$_models[$tableName.$className]))
        return self::$_models[$tableName.$className];
    else
    {
      $model=self::$_models[$tableName.$className]=new $className(null);
      $model->tableName = $tableName;

      $model->_md=new CActiveRecordMetaData($model);
      $model->attachBehaviors($model->behaviors());
      return $model;
    }
 }

Now when I make:

echo MyModel::model('tbl_history')->tableName(); // Output: tbl_history

It returns right value, but:

MyModel::model('tbl_history')->find();

still returns value for tbl.

Added:

public function __construct($id=null,$scenario=null){
    var_dump($id);
    echo '<br/>';
    parent::__construct($scenario);
}

and got:

string(tbl_history)
string(tbl_history)
NULL

It means Yii makes call to model from other place but don't know from where and how to prevent it.

Also It makes 2 calls to model, is it too bad for performance?

  • 写回答

5条回答 默认 最新

  • dongmaobeng7145 2013-05-08 19:33
    关注

    It looks like the CActiveRecord::getMetaData() method needs to be overridden to achieve what you are looking for.

    <?php
    class TestActiveRecord extends CActiveRecord
    {
        private $tableName = 'tbl'; // <=default value
        private static $_models=array();
        private $_md;
    
        public function __construct($scenario='insert', $tableName = null)
        {
    
            if($this->tableName === 'tbl' && $tableName !== null)
                $this->tableName = $tableName;
            parent::__construct($scenario);
        }
    
        public static function model($tableName = false, $className=__CLASS__)
        {
            if($tableName === null) $className=null; // this string will save internal CActiveRecord functionality
            if(!$tableName)
                return parent::model($className);
    
            if(isset(self::$_models[$tableName.$className]))
                return self::$_models[$tableName.$className];
            else
            {
                $model=self::$_models[$tableName.$className]=new $className(null);
                $model->tableName = $tableName;
    
                $model->_md=new CActiveRecordMetaData($model);
                $model->attachBehaviors($model->behaviors());
    
                return $model;
            }
        }
    
        public function tableName()
        {
            return $this->tableName;
        }
    
        /**
         * Returns the meta-data for this AR
         * @return CActiveRecordMetaData the meta for this AR class.
         */
        public function getMetaData()
        {
            if($this->_md!==null)
                return $this->_md;
            else
                return $this->_md=static::model($this->tableName())->_md;
        }
    
        public function refreshMetaData()
        {
            $finder=static::model($this->tableName());
            $finder->_md=new CActiveRecordMetaData($finder);
            if($this!==$finder)
                $this->_md=$finder->_md;
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题