Dynamic change table name in AR

Hi,

is it possible to dynamic set table name in AR before I will create object of model?

Since tableName() is a function you should be able to return any value you want. The logic for that method depends on your requirements…

And how to pass parameters to this method?I tried to override constructor but my solution not work.





class Outsite extends CActiveRecord

{

    private $_dsn;

    private $_username;

    private $_password;

    private $_tablename;


    

    public function __construct($dsn='',$username='',$password='',$tablename='')

    {

        $this->_dsn=$dsn;

        $this->_username=$username;

        $this->_password=$password;

        $this->_tablename=$tablename;

        parent::__construct();

    }

    

    public function getDbConnection()

    {

        $connection=new CDbConnection($this->_dsn,$this->_username,$this->_password);

        $connection->active=true;

        return $connection;

    }

    

    /**

     * Returns the static model of the specified AR class.

     * @return CActiveRecord the static model class

     */

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }


    /**

     * @return string the associated database table name

     */

    public function tableName()

    {

        return $this->_tablename;

    }

}







$out=new Outsite($dsn,$username,$password,'user');

$model=$out->findAll();



When I comment

//parent::__construct();

also does not work.

parent::__construct() should be the first call you make, not the last.

I get error:

Missing argument 2 for Outsite::__construct(), called in E:\www\UPI\framework\db\ar\CActiveRecord.php on line 326 and defined

This is my stack trace:




#0 E:\www\framework\db\ar\CActiveRecord.php(326): Outsite->__construct()

#1 E:\www\framework\db\ar\CActiveRecord.php(341): model()

#2 E:\www\framework\db\ar\CActiveRecord.php(59): Outsite->getMetaData()

#3 E:\www\project\protected\models\Outsite.php(37): Outsite->__construct()

#4 E:\www\project\protected\controllers\SiteController.php(46): Outsite->__construct()

#5 E:\www\framework\web\actions\CInlineAction.php(32): SiteController->actionIndex()

#6 E:\www\framework\web\CController.php(300): CInlineAction->run()

#7 E:\www\framework\web\CController.php(278): SiteController->runAction()

#8 E:\www\framework\web\CController.php(257): SiteController->runActionWithFilters()

#9 E:\www\framework\web\CWebApplication.php(320): SiteController->run()

#10 E:\www\framework\web\CWebApplication.php(120): CWebApplication->runController()

#11 E:\www\framework\base\CApplication.php(135): CWebApplication->processRequest()

#12 E:\www\project\index.php(11): CWebApplication->run()




I solved the problem by keeping the data in the user session. The solution may be not elegant but works.

http://stackoverflow…-in-a-yii-model

http://www.yiiframework.com/forum/index.php/topic/30891-model-with-dynamic-table-name/

Hi

I’ve done that in the past though I’ve run into several issues in the framework

I logged this in the bug tracking system and also opened a forum thread on it.

You can track this back throught the old and new tracking system:

https://code.google.com/p/yii/issues/detail?id=2956&can=1&q=reporter%3Amario.de.weerd%40gmail.com&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Stars%20Summary

Currently I am not using the method for several reasons; one of them is that I do not like changing the framework on each upgrade.