Reputation: 602
Hi I'm trying to use a model that will generate dynamic table name from another database. I've managed to set the table name by overriding the tableName() function. But i'm getting an error saying
The table "powerDovakin_{FUS.THUM}" for active record class "PowersTransactions" cannot be found in the database
Here is the model class in question
<?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 ;
}
}
Now i've turned on logging and the trace shows that the error is being thrown when this query is being executed.. Here are some of the relevant lines from the stack trace
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)
From the above trace what i could find out is that Yii is putting backticks (`) around the dots and maybe interpreting the portion after the dots as a column name.
My question is how can i make Yii use this sort of table names. I wish i could change the table names but my hands are tied at this moment. I just can't change them as they are not mine. So again the table names are like
powerDovakin_{FUS.THUM} , powerDovakin_{ROH.THUM}, etc
Is it possible to make the model accept such names. Please provide any sort of help as i can't find any solution to this problem. I would really appreciate any help i can get on this.
Thanks, In Advance, Maxx
Upvotes: 1
Views: 565
Reputation: 5335
the above code might give you the ability to fetch records from the tables but i don't think that you can insert any rows.
You need to call the constructor of the parent class in order to get the required functionality.
class PowersTransactions extends CActiveRecord {
public $symbol;
public function __construct ($symbol) {
$this->symbol = $symbol;
parent::__construct();
}
/**
* other code goes here
*/
}
Code above was tested and working
Upvotes: 1