Yii - 设置Active Record动态表名时出错

时间:2013-12-28 07:15:57

标签: php mysql activerecord yii

您好我正在尝试使用将从另一个数据库生成动态表名的模型。我已经设法通过覆盖tableName()函数来设置表名。但我得到一个错误说

The table "powerDovakin_{FUS.THUM}" for active record class "PowersTransactions" cannot be found in the database

这是有问题的模型类

<?php


class PowersTransactions
        extends CActiveRecord {


    public $symbol ;


    public function __construct ($symbol) {
        $this->symbol = $symbol;
    }



    /**
     * @return string the associated database table name
     */
    public function tableName () {
        return "powerDovakin_{" . $this->symbol ."}";
    }    

    /**
     * @return array relational rules.
     */
    public function relations () {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array (
                ) ;
    }


    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return InsidersTransactions the static model class
     */
    public static function model ( $className = __CLASS__ ) {
        return parent::model ( $className ) ;
    }






    /**
     * Overriding parent getDbConnection to allow for use of different database
     */
    public function getDbConnection () {

        return Yii::app ()->powersDovakin ;
    }



}

现在我打开了日志记录,跟踪显示正在执行此查询时抛出错误。以下是堆栈跟踪中的一些相关行

12:19:45.053172 trace   system.db.CDbConnection 
[ocak07jk4q3v8nfd535io8fdd4] Opening DB connection
in /var/www/html/PowerAnalysis/protected/models/PowersTransactions.php
(283)
in /var/www/html/PowerAnalysis/protected/models/PowersTransactions.php
(191)
in /var/www/html/PowerAnalysis/protected/views/realTime/_powerView.php
(9)
12:19:45.053564 trace   system.db.CDbCommand    
[ocak07jk4q3v8nfd535io8fdd4] Querying SQL: SHOW FULL COLUMNS FROM
`powerDovakin_{FUS.THUM}`

in /var/www/html/PowerAnalysis/protected/models/PowersTransactions.php
(191)
in /var/www/html/PowerAnalysis/protected/views/realTime/_powerView.php
(9)
in /var/www/html/PowerAnalysis/protected/views/realTime/view.php (715)
12:19:45.053858 error   system.db.CDbCommand    
[ocak07jk4q3v8nfd535io8fdd4] CDbCommand::fetchAll() failed:
SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT command
denied to user 'user1'@'localhost' for table 'THUM}'. The SQL statement
executed was: SHOW FULL COLUMNS FROM `powerDovakin_{FUS`.`THUM}`.
in /var/www/html/PowerAnalysis/protected/models/PowersTransactions.php
(191)
in /var/www/html/PowerAnalysis/protected/views/realTime/_powerView.php
(9)
in /var/www/html/PowerAnalysis/protected/views/realTime/view.php (715)

从上面的描述中我可以发现Yii在点周围放置反引号(`)并且可能将点后面的部分解释为列名。

我的问题是如何让Yii使用这种表名。我希望我可以改变表名,但此刻我的双手并列。我不能改变它们,因为它们不是我的。所以表名再次像

powerDovakin_{FUS.THUM} , powerDovakin_{ROH.THUM}, etc 

是否可以让模型接受这样的名称。请提供任何形式的帮助,因为我找不到任何解决此问题的方法。我真的很感激我能得到的任何帮助。

先谢谢, Maxx

1 个答案:

答案 0 :(得分:1)

上面的代码可能会让您从表中获取记录,但我不认为您可以插入任何行。

您需要调用父类的构造函数才能获得所需的功能。

    class PowersTransactions extends CActiveRecord {


        public $symbol;


        public function __construct ($symbol) {
            $this->symbol = $symbol;
            parent::__construct();
        }

        /**
         * other code goes here
         */

}

上面的代码已经过测试和运作