简体   繁体   中英

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. 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":

id
meta_id
tracks

where the meta_id of the child is equal to the id of the parent.

In old fashioned MySQL I would get all books by using:

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. 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. You'll see ZF has excellent facilities to handle this sort of thing (short of using an ORM like Doctrine).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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