php - 使用 PostgreSQL 在 Yii Framework 中设置非默认架构

标签 php postgresql yii database-schema

我将 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/

相关文章:

postgresql - Postgres 按日期和时区计数

postgresql - 将表转换为自定义类型数组

sql - 为什么我使用 LIKE '%\_' 的查询返回所有行而不仅仅是下划线结尾的行?

javascript - Yii 可点击 td

php - 内部服务器错误 500,运行 Yii 时出现内存不足错误

PHP PDO 和 MYSQL 登录代码失败

php - 如何在 Angular 和 PHP 中对某些用户隐藏页面

php - 在多个 while 循环中生成选项卡和表格

php - 无法使用php向ios发送推送通知

php - 将 Symfony2 应用程序部署到 AWS Elastic Beanstalk - 部署后缓存清除