views:

35

answers:

3

I have models of my tables and would like to do a join using a model as opposed to to a table. For example, instead of:

$select = $this->select()
 ->from(array('p' => 'products'),
   array('product_id', 'product_name'))
 ->join(array('l' => 'line_items'),
   'p.product_id = l.product_id',
 ->limit(20, 10);

where I specify the table names and columns I want to join, can I not use my models?

$select = $this->select()
 ->from(array('p' => 'products'),
   array('product_id', 'product_name'))
 ->join(array('l' => Model_Table1::tableName()),
   'p.product_id = l.product_id',
 ->limit(20, 10);
A: 

no ... u cann't join two model ... these are class which can be include .. so u can only use these class in controller or can include in another model too ... according to application or requirement

Richa
A: 

I don't see why not, if your model has a static variable with the name and a static function to return the variable:

protected static $table = 'dbname';
public static function tableName() {
    return self::$table;
}

Is it worth it though? Is the table name ever going to change?

Ashley
The models in question are controlling two tables (in order to maintain 6th Normal Form). Including just one table is not an option, and the join required is complicated.
Dickie
A: 

You must set integrity check is false. Like this

$select = $this->select()
 ->setIntegrityCheck(false)
 ->from(array('p' => 'products'),
   array('product_id', 'product_name'))
 ->join(array('l' => 'line_items'),
   'p.product_id = l.product_id',
 ->limit(20, 10);
scopus