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

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

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

class PowersTransactions extends CActiveRecord {

public $symbol;

public function __construct ($symbol) {
$this->symbol = $symbol;
parent::__construct();
}
/**
* other code goes here
*/
}

上面的代码已经过测试并且正在运行

最新更新