Symfony 项目的模型存储在多个数据库中

发布于 2024-11-11 06:02:09 字数 504 浏览 4 评论 0原文

我正在编写 Symfony 项目(使用 symfony 1.4 版本,以 Propel 作为其 ORM),其中一些数据存储在 MySQL 数据库中,其他一些数据存储在另一台服务器上的 PostgreSQL 数据库中。

更准确地说,我想同时将一些模型存储在 MySQL 数据库中,将其他模型存储在 PostgreSQL 数据库中,并且无需显式数据库切换即可无缝完成(我的意思是 Propel 将使用正确的数据库连接和 SQL 方言来检索/存储数据)。 MySQL部分的模型不会与PostgreSQL有关系。

是否可以?如果是,我还想知道如何设置开发环境(我想在开发和生产环境中访问不同的 MySQL/PostgreSQL 数据库)。

UPD:我发现了有关此问题的问题: Symfony 中的多个数据库支持但我必须检查它是否适用于最新版本的 Symfony。

I am writing Symfony project (using symfony 1.4 ver. with Propel as its ORM) where some data is stored in MySQL database and some other data is stored on another server in PostgreSQL database.

To be more precise I want to store some models in MySQL database and other models in PostgreSQL database at the same time and do it seamlessly without explicit database switching (I mean Propel will use proper database connection and SQL dialect to retrieve/store data). Models from MySQL part will not have relations with PostgreSQL.

Is it possible? If yes I also would like to know how to setup development environment (I want to access different MySQL/PostgreSQL DBs in developement and production environments).

UPD: I've found question on SO reagrding this problem: Multiple databases support in Symfony But i have to check if it works with recent versions of Symfony.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风蛊 2024-11-18 06:02:09

我每天都使用 symfony,事实上你可以有 2 个数据库来存储模型的不相关部分。你需要在你的database.yml中设置两个连接(我不熟悉posgress,所以你必须弄清楚如何正确设置它):

  mysql_connection:
    class:        sfPropelDatabase
    param:
      phptype:    mysql
      classname:  MysqlPropelPDO
      dsn:        ''
      username:   user
      password:   pass
      encoding:   UTF-8
      pooling:    true

  postgress_connection:
    class:        sfPropelDatabase
    param:
      phptype:    postgres
      classname:  PostgresPropelPDO
      dsn:        ''
      username:   user
      password:   pass
      encoding:   UTF-8
      pooling:    true

完成后,我们应该开始使用schema.yml文件或文件(因为您将使用 2 个数据库,我建议有 2 个文件,一个用于 mysql,另一个用于 postgres 数据库):

mysql_schema.yml 文件:

//This is how you tell witch connection you are using for this tables
connection: mysql_connection 

classes:
  CLassName:
    tableName: table_name
    columns:
      id:
      name:
        type: varchar(255)
        required: true
  [...]

postgres_schema.yml 文件:

connection:  postgress_connection
    classes:
      ClassName:
        tableName: table_name
        columns:
          id:
          name:
            type: varchar(255)
            required: true
      [...]

一旦您完成了模式文件的设置,您就可以开始创建所有类并开始享受乐趣了。希望这有帮助

i work with symfony every day and in fact you can have 2 databases in order to store unrelated parts of the model. You need to set up both connection in you database.yml (i'm unfamiliar with posgress so you will have to figure out how to set it up correclty):

  mysql_connection:
    class:        sfPropelDatabase
    param:
      phptype:    mysql
      classname:  MysqlPropelPDO
      dsn:        ''
      username:   user
      password:   pass
      encoding:   UTF-8
      pooling:    true

  postgress_connection:
    class:        sfPropelDatabase
    param:
      phptype:    postgres
      classname:  PostgresPropelPDO
      dsn:        ''
      username:   user
      password:   pass
      encoding:   UTF-8
      pooling:    true

Once you have done that, we should get started with the schema.yml file or files (as you will be using 2 databases i would suggest to have 2 files, one for the mysql and another for the postgres database):

mysql_schema.yml file:

//This is how you tell witch connection you are using for this tables
connection: mysql_connection 

classes:
  CLassName:
    tableName: table_name
    columns:
      id:
      name:
        type: varchar(255)
        required: true
  [...]

postgres_schema.yml file:

connection:  postgress_connection
    classes:
      ClassName:
        tableName: table_name
        columns:
          id:
          name:
            type: varchar(255)
            required: true
      [...]

Once you have finished setting up your schema files, you should be good to go, create all classes and start to have fun. Hope this helps

默嘫て 2024-11-18 06:02:09

我相信你可以!

谷歌对此有很多结果,请尝试

http://snippets.symfony-project.org/snippet /194

这是基于旧版本的 propel/symfony,但快速浏览一下,我相信它仍然有效。另外,最近有评论表明它有效。

I believe you can!

Google has quite a few results for this, try

http://snippets.symfony-project.org/snippet/194

Thats based on an older version of propel/symfony, but from a quick look, I believe it's still valid. Plus there are recent comments suggesting it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文