Frage

Do a small component for my Yii app. Code

$connection = Yii::app()->getComponent('db');
$sql = 'SELECT * FROM {{settings}}';
$command = $connection->createCommand($sql); 

raises error

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{settings}}' at line 1. The SQL statement executed was: SELECT * FROM {{settings}}

Tell me what went wrong. I can't use braces to escape the table name?

Thanks

UPDATE:

It seem this is Yii Query Bulduer bug.

if I use this configuration

'db'=>array(
    'connectionString' => '...',
    'emulatePrepare' => ...,
    'username' => '...',
    'password' => '...',
    'charset' => '...',
    'tablePrefix' => ''
),

pay attention to the empty tablePrefix — all works fine.

Removing this key I get the problem described above. But I don't need table prefix. I just want to use a component that can work with prefixes, not only in my application.

War es hilfreich?

Lösung

Sorry, I've just realised what your update actually says.

if I use this configuration

'db'=>array(
    'connectionString' => '...',
    'emulatePrepare' => ...,
    'username' => '...',
    'password' => '...',
    'charset' => '...',
    'tablePrefix' => ''
),

pay attention to the empty tablePrefix — all works fine.

Yes, of course it does: you have defined tablePrefix to be an empty string (and, as I already pointed out, empty-string prefixes have been supported since v.1.1.6).

Removing this key I get the problem described above. But I don't need table prefix. I just want to use a component that can work with prefixes, not only in my application.

If you don't define tablePrefix, then comparing it against null will be true (and as I also pointed out before, CDbCommand::setText() replaces non-null prefixes):

if($this->_connection->tablePrefix!==null && $value!='')
    $this->_text=preg_replace('/{{(.*?)}}/',$this->_connection->tablePrefix.'\1',$value);

Thus you must either replace braces yourself or else explicitly set tablePrefix to be the empty string. If you have no control over the configuration file, but want to ensure that tablePrefix is set, you could manually override it:

if (!isset($connection->tablePrefix)) $connection->tablePrefix = '';
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top