1

Two models are returned as Zend_Db_Select objects.

Then, I need to join them and fetch data together at once.

class Model_User extends Abstract_Model { 
    public function sqlUser() { 
    return $this->select(array(
               'user_id', 'user.name', 'user.login', 'address.street', 'city.city_id', 'city.city_name','region.region_id', 'region.region_name'
            )) 
            ->joinUsing('address','address_id','') ->join('city', 'city.city_id = address.city_id', '')
            ->join('region', 'region.region_id = city.region_id', ''); 
    } 
} 

class Model_Technics extends Abstract_Model{ 
    public function sqlList() { 
        return $this->select()
                 // here some more sql
                 ->joinUsing('catalog_model','model_id',''); 
    }
}

Then I need some where else fetch sqlList with all info for every user. I dont whant to duplicate all code, I just want to join sql from User model through join

Crusader
  • 1,182
  • 11
  • 22
  • 1
    it would be much easier to answer your question if you provided a relevant snippet of code – Don Aug 10 '12 at 14:40
  • 1
    I'm gonna suggest that you might want to either implement some kinda mapper strategy or just pick 1 table and execute a query by joining the other tables to it. What you have now is disturbing. – RockyFord Aug 10 '12 at 16:04
  • 1
    You can use findDependentRowset to get bove results at one select, maybe it can help you: http://framework.zend.com/manual/en/zend.db.table.relationships.html/ – digoferra Aug 16 '12 at 13:25

1 Answers1

0

You could iterate through the "sections" of zend_db_select (columns,from,joins,where) and figure a way to append them to the other query, but I wouldn't suggest it.

An alternative might be to have a "join" method on the model you're joining to, which will take your select object and run the join method(s) on it. It would, of course, depend on the table(s) already available in the select object already. Or that method might just pass back a structure defining how to join to the join-model's table, which your primary model can decide what to do with it.

There are a ton of solutions, but there's no really easy way to just jam two zend_db_select objects together without some extra logic between them. The main missing piece would be relationship information between the two models.

I know this isn't a full blown answer (as I can't comment yet), but hopefully it will point you to a path that sounds usable.

Gordon Forsythe
  • 356
  • 3
  • 7