gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:21:39 27 4
gpt4 key购买 nike

您好,我正在尝试使用一个模型,该模型将从另一个数据库生成动态表名。我已经设法通过覆盖 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 

是否可以让模型接受这样的名字。请提供任何帮助,因为我找不到解决此问题的方法。如果我能在这方面得到任何帮助,我将不胜感激。

提前致谢,麦克斯

最佳答案

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

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

    class PowersTransactions extends CActiveRecord {


public $symbol;


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

/**
* other code goes here
*/

}

上面的代码已经过测试并且可以正常工作

关于php - Yii - 设置 Active Record 动态表名时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20812064/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com