Symfony Doctrine 数据库架构与不同数据库的关系

发布于 2024-11-26 11:48:26 字数 127 浏览 0 评论 0原文

我在设计 Doctrine 数据库模式时遇到了这个问题。假设我有2个数据库,A和B。

我已经制作了数据库A模式,现在我需要制作数据库B模式。在数据库B中,其中一张表与数据库A的表有关系。这就是问题所在,如何将B与A关联起来?

I'm having this problem about designing Doctrine database schema. Assume I have 2 databases, A and B.

I've already make database A schema, and now I need to make database B schema. In database B, one of the tables has relation to database A's table. This is the problem, how can I relate B to A?

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

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

发布评论

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

评论(2

始终不够爱げ你 2024-12-03 11:48:26

@Dziamid 说对了一半。

从技术上讲,您无法将两个表连接到单独的数据库。但你可以假装任何真正的干预。

配置多个数据库连接:

//databases.yml
all:
  items_db:
    class:          sfDoctrineDatabase
    param:
      dsn:          mysql://login:passwd@localhost/items
  stores_db:
    class:          sfDoctrineDatabase
    param:
      dsn:          mysql://login:passwd@localhost/stores

为每个模型定义正确的连接

//schema.yml
Item:
  connection: items_db
  columns:
    store_id: integer(4)
  relations:
    Store:
      local: store_id
      foreign: id
      foreignAlias: Items

Store:
  connection: stores_db
  columns:
    name: string(255)

现在您可以像平常一样使用 Doctrine 模型:

// like this
$item = new Item();
$store = new Store();
$store->save();
$item->setStore($store);

// or like this
$item->getStore(); 

唯一的限制是您不能在 DQL 查询中进行联接。

$query = Doctrine_Query::create()
    ->from('Store s')
    ->leftJoin('s.Items i')
    ->fetchAll();

但是您可以使用 Doctrine_Collections 加载关系。

$stores = Doctrine::getTable('Store')->findAll(); // this returns a Doctrine_Collection
$stores->loadRelated('Items');

这与 Doctrine_Query 的工作方式相同。

@Dziamid is half right.

You technically cannot join two tables over to separate database. But you can fake it would any real intervention.

Configure multiple database connections:

//databases.yml
all:
  items_db:
    class:          sfDoctrineDatabase
    param:
      dsn:          mysql://login:passwd@localhost/items
  stores_db:
    class:          sfDoctrineDatabase
    param:
      dsn:          mysql://login:passwd@localhost/stores

Define the proper connection for each model

//schema.yml
Item:
  connection: items_db
  columns:
    store_id: integer(4)
  relations:
    Store:
      local: store_id
      foreign: id
      foreignAlias: Items

Store:
  connection: stores_db
  columns:
    name: string(255)

Now you can use your Doctrine models as you normally would:

// like this
$item = new Item();
$store = new Store();
$store->save();
$item->setStore($store);

// or like this
$item->getStore(); 

The only limitation is that you CANNOT do joins in DQL queries.

$query = Doctrine_Query::create()
    ->from('Store s')
    ->leftJoin('s.Items i')
    ->fetchAll();

But you can load relations using from Doctrine_Collections.

$stores = Doctrine::getTable('Store')->findAll(); // this returns a Doctrine_Collection
$stores->loadRelated('Items');

This works the same as the Doctrine_Query.

等风也等你 2024-12-03 11:48:26

不同数据库中的表之间不能有关系。如果这样做,您最终会遇到外键约束错误。然而,您可以做的是保留一个裸露的relation_id字段并从另一个连接手动加载相关数据。例如:

Item:
  columns:
    store_id: integer(4)
  #relations:
  #  Store:
  #    local: store_id
  #    foreign: id
  #    foreignAlias: Items

Store:
  columns:
    name: string(255)

class Item extends BaseItem
{
  protected $_store = null;

  public function getStore()
  {
    if (null == $this->_store)
    {
      $this->_store = Doctrine::getTable('Store')->findOneById($this->store_id);
    }
    return $this->_store;
  }

  public function setStore(Store $store)
  {
    $this->store_id = $store->id;
  }
}

现在您可以使用 Item 和 Store,就好像它们是相关的一样:

$item = new Item();
$store = new Store();
$store->save();
$item->setStore($store);

You can't have a relation between tables in different databases. If you do, you will end up with a foreign key constraint error. What you can do, however, is to leave a bare relation_id field and manually load related data from another connection. For example:

Item:
  columns:
    store_id: integer(4)
  #relations:
  #  Store:
  #    local: store_id
  #    foreign: id
  #    foreignAlias: Items

Store:
  columns:
    name: string(255)

class Item extends BaseItem
{
  protected $_store = null;

  public function getStore()
  {
    if (null == $this->_store)
    {
      $this->_store = Doctrine::getTable('Store')->findOneById($this->store_id);
    }
    return $this->_store;
  }

  public function setStore(Store $store)
  {
    $this->store_id = $store->id;
  }
}

Now you can work with Item and Store as if they were related:

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