Changeset 3109

Show
Ignore:
Timestamp:
04/13/08 08:45:48 (3 months ago)
Author:
pmjones
Message:

Solar_Sql_Model_Related_HasMany: [CHG] Better grouping of internal logic.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Solar/Sql/Model/Related/HasMany.php

    r3107 r3109  
    3737    { 
    3838        if (empty($this->through)) { 
    39             // less-complex "has many" relationship. 
     39            // less-complex "has many" relationship. use the standard support 
     40            // method. 
    4041            $this->_modSelectEager($select); 
     42        } else { 
     43            // more-complex "has many through" relationship. 
     44            // join the native table to the mapping table. 
     45            $table = "{$this->through_table} AS {$this->through_alias}"; 
     46            $where = "{$this->native_alias}.{$this->native_col} = " 
     47                   . "{$this->through_alias}.{$this->through_native_col}"; 
     48         
     49            $select->leftJoin($table, $where); 
    4150             
     51            // join the mapping table to the foreign table. 
     52            $table = "{$this->foreign_table} AS {$this->foreign_alias}"; 
     53            $where = "{$this->through_alias}.{$this->through_foreign_col} = " 
     54                   . "{$this->foreign_alias}.{$this->foreign_col}"; 
     55         
     56            $select->leftJoin($table, $where); 
     57         
    4258            // make the rows distinct, so we only get one row regardless of 
    4359            // the number of related rows (since we're not selecting cols). 
    4460            $select->distinct(true); 
    45              
    46             // done! 
    47             return; 
    48         } 
    49          
    50         // more-complex "has many through" relationship. 
    51         // join the native table to the mapping table. 
    52         $join_table = "{$this->through_table} AS {$this->through_alias}"; 
    53         $join_where = "{$this->native_alias}.{$this->native_col} = " 
    54                     . "{$this->through_alias}.{$this->through_native_col}"; 
    55          
    56         $select->leftJoin($join_table, $join_where); 
    57          
    58         // join the mapping table to the foreign table. 
    59         $join_table = "{$this->foreign_table} AS {$this->foreign_alias}"; 
    60         $join_where = "{$this->through_alias}.{$this->through_foreign_col} = " 
    61                     . "{$this->foreign_alias}.{$this->foreign_col}"; 
    62          
    63         $select->leftJoin($join_table, $join_where); 
     61         
     62            // honor foreign inheritance 
     63            if ($this->foreign_inherit_col) { 
     64                $select->where( 
     65                    "{$this->foreign_alias}.{$this->foreign_inherit_col} = ?", 
     66                    $this->foreign_inherit_val 
     67                ); 
     68            } 
     69        } 
    6470         
    6571        // make the rows distinct, so we only get one row regardless of 
    6672        // the number of related rows (since we're not selecting cols). 
    6773        $select->distinct(true); 
    68          
    69         // honor foreign inheritance 
    70         if ($this->foreign_inherit_col) { 
    71             $select->where( 
    72                 "{$this->foreign_alias}.{$this->foreign_inherit_col} = ?", 
    73                 $this->foreign_inherit_val 
    74             ); 
    75         } 
    7674    } 
    7775     
     
    9391    { 
    9492        if (empty($this->through)) { 
    95             // less-complex "has many" relationship. 
    96             return parent::modSelectCountPages($select); 
    97         } 
    98          
    99         // more-complex "has many through" relationship. 
    100         // join the native table to the mapping table. 
    101         $join_table = "{$this->through_table} AS {$this->through_alias}"; 
    102         $join_where = "{$this->native_alias}.{$this->native_col} = " 
    103                     . "{$this->through_alias}.{$this->through_native_col}"; 
    104          
    105         $select->leftJoin($join_table, $join_where); 
    106          
    107         // join the mapping table to the foreign table. 
    108         $join_table = "{$this->foreign_table} AS {$this->foreign_alias}"; 
    109         $join_where = "{$this->through_alias}.{$this->through_foreign_col} = " 
    110                     . "{$this->foreign_alias}.{$this->foreign_col}"; 
    111          
    112         $select->leftJoin($join_table, $join_where); 
    113          
    114         // honor foreign inheritance 
    115         if ($this->foreign_inherit_col) { 
    116             $select->where( 
    117                 "{$this->foreign_alias}.{$this->foreign_inherit_col} = ?", 
    118                 $this->foreign_inherit_val 
    119             ); 
    120         } 
     93            // less-complex "has many" relationship. use the standard parent 
     94            // method. 
     95            parent::modSelectCountPages($select); 
     96        } else { 
     97            // more-complex "has many through" relationship. 
     98            // join the native table to the mapping table. 
     99            $table = "{$this->through_table} AS {$this->through_alias}"; 
     100            $where = "{$this->native_alias}.{$this->native_col} = " 
     101                   . "{$this->through_alias}.{$this->through_native_col}"; 
     102         
     103            $select->leftJoin($table, $where); 
     104         
     105            // join the mapping table to the foreign table. 
     106            $table = "{$this->foreign_table} AS {$this->foreign_alias}"; 
     107            $where = "{$this->through_alias}.{$this->through_foreign_col} = " 
     108                   . "{$this->foreign_alias}.{$this->foreign_col}"; 
     109         
     110            $select->leftJoin($table, $where); 
     111         
     112            // honor foreign inheritance 
     113            if ($this->foreign_inherit_col) { 
     114                $select->where( 
     115                    "{$this->foreign_alias}.{$this->foreign_inherit_col} = ?", 
     116                    $this->foreign_inherit_val 
     117                ); 
     118            } 
     119        } 
     120         
     121        // make the rows distinct, so we only get one row regardless of 
     122        // the number of related rows (since we're not selecting cols). 
     123        $select->distinct(true); 
    121124    } 
    122125