我将 Yii Framework 与 PostgreSQL 结合使用,我需要在非默认架构中创建新模型。我怎样才能做到这一点? 我尝试了以下方法:
protected/components/PgSchemaConnection.php
class PgSchemaConnection extends CDbConnection {
protected function initConnection($pdo)
{
parent::initConnection($pdo);
$stmt=$pdo->prepare("set search_path to master, public");
$stmt->execute();
}
}
protected/config/main.php
return array(
...
'db'=>array(
'connectionString' => 'pgsql:host=localhost;dbname=bsc',
'username' => 'aUser',
'password' => 'aPass',
'charset' => 'utf8',
'class' => 'PgSchemaConnection'
),
...
)
但是当我在控制台中运行时
>> model Foo
Warning: the table 'Foo' does not exist in the database.
generate models/relatedobjective.php
PHP Warning: Invalid argument supplied for foreach() in /home/cristhian/php_apps/yii-1.1.14.f0fee9/framework/cli/views/shell/model/fixture.php on line 13
PHP Warning: Invalid argument supplied for foreach() in /home/cristhian/php_apps/yii-1.1.14.f0fee9/framework/cli/views/shell/model/fixture.php on line 19
generate fixtures/relatedobjective.php
generate unit/relatedobjectiveTest.php
The following model classes are successfully generated:
relatedobjective
If you have a 'db' database connection, you can test these models now with:
$model=relatedobjective::model()->find();
print_r($model);
Yii 没有在数据库中找到 Foo 表(很明显,我已经创建了 Foo 表)
如何设置另一个模式?我做错了什么?
最佳答案
只需提供表的完整路径 - 包括架构名称。
对于 gii,我将“tableName”字段设置为“schemaName.tableName”。
您的模型将如下所示:
class MyModel extends CActiveRecord{
...
tableName(){
return 'schemaName.tableName';
}
此模型将使用您的默认“db”连接配置执行查询。
关于php - 使用 PostgreSQL 在 Yii Framework 中设置非默认架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354157/