Yii Framework Forum: Dynamic change table name in AR - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dynamic change table name in AR Rate Topic: -----

#1 User is offline   qwerty 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 290
  • Joined: 20-November 08

Posted 18 January 2010 - 08:27 PM

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

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 19 January 2010 - 03:26 AM

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

#3 User is offline   qwerty 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 290
  • Joined: 20-November 08

Posted 19 January 2010 - 04:43 AM

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

0

#4 User is offline   qwerty 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 290
  • Joined: 20-November 08

Posted 19 January 2010 - 04:49 AM

When I comment

//parent::__construct();

also does not work.

0

#5 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 19 January 2010 - 04:53 AM

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

#6 User is offline   qwerty 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 290
  • Joined: 20-November 08

Posted 19 January 2010 - 06:16 AM

I get error:

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

#7 User is offline   qwerty 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 290
  • Joined: 20-November 08

Posted 19 January 2010 - 06:20 AM

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


0

#8 User is offline   qwerty 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 290
  • Joined: 20-November 08

Posted 19 January 2010 - 07:24 PM

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

#9 User is offline   Giancarlo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 242
  • Joined: 20-December 11
  • Location:Turin, Italy

Posted 28 March 2013 - 10:33 AM

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

http://www.yiiframew...mic-table-name/
Posted Image Non preoccuparti, รจ dentro il monitor!

Sapevi che cercando su google yii + "la funzione su cui stai impazzendo", trovi quasi sempre dei buoni indizi in italiano, per risolvere i tuoi problemi, senza perdere tempo a postare?
0

#10 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 325
  • Joined: 08-June 10
  • Location:France

Posted 29 March 2013 - 03:55 PM

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....Stars%20Summary
https://github.com/y.../yii/issues/172

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.
0

#11 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 890
  • Joined: 19-February 13
  • Location:India

Posted 20 January 2014 - 07:30 AM

Posted Image
Thanks,
Ankit Modi
Skype : amodi06
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users