Search code examples
zend-frameworkzend-db-select

Zend Framework: Select data from diffrent tables


I want to create a search engine in my site, but have one problem can I select all values from two or more different tables with one query, because have one category exp. News and in this category have subcategories and every subcategory have other tables and if I select News I need to get all values from subcategories tables.

Exp. tables: first table name is test1, second table name is test2:

id|title|content|
1|dog|dog is big|
2|cat|cat is small|

id|title|content|
1|plant|plant1|
2|stone|stone1|

And I need to get all values from this tables.


Solution

  • Well, looks like you need to build a union here:

    $db = Zend_Db::factory( ...options... );
    $select = $db->select()
        ->union(array(
            $db->select()
                ->from(array('t1' => 'test1'))
                ->where(/* search criteria goes here */),
            $db->select()
                ->from(array('t2' => 'test2'))
                ->where(/* search criteria goes here as well */)
            ));
    

    Alternatively, you may want to look at Sphinx.