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

查看:26
本文介绍了如何在 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' => 'yiidbConnection',
    'dsn' => 'pgsql:host=localhost;dbname=db_name', 
    'username' => 'db_username',
    'password' => 'db_password',
    'charset' => 'utf8',
    'schemaMap' => [
      'pgsql'=> [
        'class'=>'yiidbpgsqlSchema',
        'defaultSchema' => 'public' //specify your schema here
      ]
    ], // PostgreSQL
];

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

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