2010年4月26日星期一

[fw-db] Combine two one-to-many relationship

Hi,

 

 

Is there any way I can combine two one-to-many relationship?

For example, a world region has many countries, a country has many institutions. How could I find all cities in a world region?

What I am doing now is find all countries in a world region, then find the institutions in every country, then merge these institutions together.

I don’t think this is a elegant way.

 

Of course I can using the following SQL to find all institutions. My Question is there a Zend_Db_Tables way to do it?

 

Select * from institions as i where i.country_id in (select c.country_id from countries as c where c.continent_id = 1)

 

 

 

------------- code to find all institions in a continents ------------------------------------------

 

            public function getAllInstitutionsInCountry(Application_Model_Country $country){

                        return $country->findDependentRowset( 'Application_Model_Institutions' )->toArray();

            }

public function getAllInstitutionsInContinent(Application_Model_Continent $continent)

            {

                        $institutions = new Application_Model_Institutions();

            $countires = new Application_Model_Countries();

                        $countries $countires->getAllContiresInContinent($continent);

                        $allInstitutions = array();

                        foreach ($countries as $country) {

                                    $allInstitutions = array_merge($allInstitutions, $institutions->getAllInstitutionsInCountry($country));

                        }

                        return $allInstitutions;

}

 

 

 

 

Thanks

 

 

Ting       

没有评论: