如何在Yii2中设置默认架构 [英] How to set default schema in Yii2

查看:86
本文介绍了如何在Yii2中设置默认架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Yii2是使用PostgreSQL设置的.我喜欢为每个项目使用架构,而不是为每个项目使用单独的数据库.稍后安装的问题是我无法弄清楚如何通过配置选择默认架构"defaultSchema".

My Yii2 is setup with PostgreSQL. Instead of using separate database per project, I like to use schema for each project. Problem with later setup is that I can't figure out how to select default schema "defaultSchema" through configuration.

我在迁移表时遇到问题,因为当我运行迁移命令时,它默认为公共"架构.默认的公共"模式还阻止使用数据库用户的search_path.尽管我使用"search_path = myschema,public"设置了数据库用户,但如果没有其他配置,我仍然无法使用迁移,因为在运行时,Yii会在表名中查找模式,如果未提供,则会回退到defaultSchema,所以无论如何您在数据库用户的search_path中拥有的内容仍将使用"public.migrations".

I am having a problem with migrations table because it defaults to "public" schema when I run migration command. Default "public" schema also prevents using database user's search_path. Although I set up my db user with "search_path=myschema, public" I still cannot use migrations without additional configuration, because during runtime Yii looks for the schema in the table name and if it is not provided falls back to the defaultSchema so no matter what you have in the database user's search_path it will still use "public.migrations".

在Yii2中设置默认架构的最佳方法是什么?是否为架构选择指定了任何配置参数?毕竟,每个连接都将使用一个模式,最好通过连接配置进行设置.

What is the best way of setting default schema in Yii2? Is there any configuration parameter designated for schema selection? After all each connection will use one schema and it would be nice to set it through the connection configuration.

推荐答案

尝试使用db.php的此变体指定defaultSchema

try this variant of db.php to specify defaultSchema

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'pgsql:host=localhost;dbname=db_name', 
    'username' => 'db_username',
    'password' => 'db_password',
    'charset' => 'utf8',
    'schemaMap' => [
      'pgsql'=> [
        'class'=>'yii\db\pgsql\Schema',
        'defaultSchema' => 'public' //specify your schema here
      ]
    ], // PostgreSQL
];

这篇关于如何在Yii2中设置默认架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆