简体   繁体   English

在ZEND中的一个查询中访问多个数据库表

[英]Accessing multiple database tables in one query in ZEND

I have a model that contains several types of products that are all stored in different MySQL databases, but all have one "parent" product that is stored in another table. 我有一个模型,其中包含几种类型的产品,这些产品都存储在不同的MySQL数据库中,但是所有产品都有一个“父”产品,该产品存储在另一个表中。 The parent table is called "products" and contains amongst others the variables: 父表称为“产品”,其中包含变量:

id
type
price
name

An example of "children" would be "books" which would contain amongst others: “孩子”的一个例子是“书籍”,其中包括:

id
meta_id
pages

Another "child" could be "dvds": 另一个“孩子”可以是“ dvds”:

id
meta_id
tracks

where the meta_id of the child is equal to the id of the parent. 子代的meta_id等于父代的id。

In old fashioned MySQL I would get all books by using: 在老式的MySQL中,我将使用以下方法获得所有书籍:

SELECT 
  p.id, p.type, p.price, p.name, b.pages 
FROM 
  products p 
LEFT JOIN 
  books b
ON
  p.id=o.meta_id

I know how to read & write data to & from one database table using Zend, extending the Zend_Db_Table_Abstract , and using a Mapper & a Model. 我知道如何使用Zend,扩展Zend_Db_Table_Abstract以及使用Mapper和Model从一个数据库表读写数据。 I'm just not sure how to do this if I have to read/write objects that are stored in multiple database tables. 如果我必须读取/写入存储在多个数据库表中的对象,我不确定如何执行此操作。 How do I set this up? 我该如何设置? What model/pattern should I use? 我应该使用什么模型/图案? I'm sure this is pretty standard stuff, but I been searchjing for days for clear examples, and I just can't seem to figure it out. 我敢肯定这是很标准的东西,但是我已经搜索了好几天了,以寻找清晰的示例,但我似乎无法弄清楚。

I had exactly the same confusion as you, and there is a great page here - Zend Framework Data Models - which explains how to solve this exact problem. 我和您完全一样困惑,这里有一个很棒的页面-Zend Framework Data Models-解释了如何解决这个确切的问题。 You'll see ZF has excellent facilities to handle this sort of thing (short of using an ORM like Doctrine). 您会看到ZF具有出色的工具来处理这种事情(缺少使用像Doctrine这样的ORM)。

Also, when you're querying multiple tables, it is useful to be aware of the integrity check, as mentioned here Zend Framework Db Select Join table help 另外,在查询多个表时,了解完整性检查很有用,如此处所述。Zend Framework Db Select Join table help

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM