开发者

Symfony Many-to-Many Relationship with External Database

开发者 https://www.devze.com 2023-01-29 07:38 出处:网络
In my application, I need to query a table in an external database database, which is linked to my default database by a many to many relationship. My intermediate table is situated in my default data

In my application, I need to query a table in an external database database, which is linked to my default database by a many to many relationship. My intermediate table is situated in my default database.

In order to do this, I modified my database.yml to include the external database. I then added to my schema.yml both the intermediary table (message_news_rel, situated in my default database), and the table from the external source (news).

Here's how I specified the relationships:

MessageNews:
  connection: uda_iPoste
  tableName: message_news_rel
  columns:
    message_id:
      type: integer(10)
      primary: true
    news_id:
      type: integer(10)
      primary: true
  relations:
    Message:
      local: message_id
      foreign: id
      foreignAlias: MessageNewses
    News:
      local: news_id
      foreign: id
      foreignAlias: MessageNewses

News:
...
  relations:
    Messages:
      class: Message
      foreignAlias: Newses
      refClass: MessageNews
      local: news_id
      foreign: message_id

Message:
...
  relations:
    Newses:
      class: News
      foreignAlias: Messages
      refClass: MessageNews
      local: message_id
      foreign: news_id

When I try using the model in my application, I get this error:

Base table or view not found: 1146 Table 'uda_web_dev.message_news_rel' doesn't exist

...referring no doubt to this part of the query:

FROM news n LEFT JOIN message_news_rel 

This is leaving me to assume that Symfony didn't switch the开发者_如何学JAVA connection from my default database to my external database when attempting to make the relationship.

Please let me know if I left out any important details.

Is there a known fix for this problem?


If you think about it rationally, what your asking is a bit to much. Every query or transaction should involve only tables that exists on the database that your going to execute it. Dont forget that the database abstraction always has its limitations, and if you check each implementation, they abstract the query creation and manipulation, but i dont see how they cant abstract databases.

So if you absolutely have to split your databases, then implement some kind of dabatase cluster. Else, implement tables in a query are located in the same database and then join results in symfony.

0

精彩评论

暂无评论...
验证码 换一张
取 消