Yii多个表的一个模型

Nar*_*rek 6 php yii

我有Yii应用程序和两个具有相同结构的表tbltbl_history:

现在想要创建模型,因此它将在调用模型时通过我发送的参数选择表.例如:

MyModel::model('tbl')->find();
//and
MyModel::model('tbl_history')->find();
Run Code Online (Sandbox Code Playgroud)

在Yii论坛中找到解决方案的相关文章.做了相同的更改,最后在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;
    }
 }
Run Code Online (Sandbox Code Playgroud)

现在当我做:

echo MyModel::model('tbl_history')->tableName(); // Output: tbl_history
Run Code Online (Sandbox Code Playgroud)

它返回正确的值,但是:

MyModel::model('tbl_history')->find();
Run Code Online (Sandbox Code Playgroud)

仍然返回tbl的值.

添加:

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

得到了:

string(tbl_history)
string(tbl_history)
NULL
Run Code Online (Sandbox Code Playgroud)

这意味着Yii其他地方调用模型,但不知道从哪里以及如何防止它.

它也会对模型进行2次调用,这对性能来说太糟糕了吗?

Kev*_*ins 5

看起来该CActiveRecord::getMetaData()方法需要被覆盖以实现您正在寻找的东西.

<?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;
    }

}
Run Code Online (Sandbox Code Playgroud)