drq1257
2010-12-06 19:59
采纳率: 0%
浏览 59
已采纳

Symfony与外部数据库的多对多关系

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 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?

图片转代码服务由CSDN问答提供 功能建议

在我的应用程序中,我需要查询外部数据库数据库中的表,该数据库链接到我的默认数据库 多对多的关系。 我的中间表位于我的默认数据库中。

为了做到这一点,我修改了我的database.yml以包含外部数据库。 然后我在schema.yml中添加了中间表(message_news_rel,位于我的默认数据库中)和来自外部源(新闻)的表。

以下是我如何指定关系:

  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_id 
 foreign:id 
 foreignAlias:MessageNewses 
 
News:
 ... 
关系:
消息:
类:消息
 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 
   
 
 

当我在我的应用程序中尝试使用该模型时,出现此错误:

 找不到基表或视图:1146表'uda_web_dev.message_news_rel'不存在 
   
 
 

...毫无疑问地提到了这部分查询:

  FROM news n LEFT JOIN message_news_rel  
   
 
 

这使我假设Symfony在尝试建立关系时没有将连接从我的默认数据库切换到我的外部数据库。

如果我遗漏了任何重要细节,请与我们联系。

此问题是否已解决?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题