Example #1
0
 /**
  * 
  * Modifies the native fetch with an eager join so that the foreign table
  * is joined properly and foreign columns are selected.
  * 
  * @param Solar_Sql_Model_Params_Eager $eager The eager params.
  * 
  * @param Solar_Sql_Model_Params_Fetch $fetch The native fetch params.
  * 
  * @return void
  * 
  * @see modEagerFetch()
  * 
  */
 protected function _modEagerFetch($eager, $fetch)
 {
     // the basic join array
     $join = array('type' => strtolower($eager['join_type']), 'name' => "{$this->foreign_table} AS {$eager['alias']}", 'cond' => array(), 'cols' => null);
     // standard to-one condition (works for both has-one and belongs-to)
     $join['cond'][] = "{$fetch['alias']}.{$this->native_col} = " . "{$eager['alias']}.{$this->foreign_col}";
     // foreign and eager conditions
     $join['cond'] = array_merge($join['cond'], $this->getForeignConditions($eager['alias']), (array) $eager['conditions']);
     // what columns to fetch?
     if (!$eager['cols']) {
         $cols = null;
     } else {
         $cols = array();
         foreach ($eager['cols'] as $col) {
             $cols[] = "{$col} AS {$eager['cols_prefix']}__{$col}";
         }
     }
     // add the columns
     $join['cols'] = $cols;
     // add the join to the parent fetch
     $fetch->join($join);
 }
Example #2
0
 /**
  * 
  * Returns an INNER JOIN specification for joining to the native table as
  * as sub-SELECT.
  * 
  * @param Solar_Sql_Model_Params_Eager $eager The eager params.
  * 
  * @param Solar_Sql_Model_Params_Fetch $fetch The native fetch settings.
  * 
  * @param string $col The foreign column to join against.
  * 
  * @return array A join specification array.
  * 
  */
 protected function _getNativeBySelect($eager, $fetch, $col)
 {
     // get a *copy* of the fetch params; don't want to mess them up
     // for other eagers. only use the joins marked "keep".
     $clone = $fetch->cloneForKeeps();
     // reset the column list and get only the native column
     $clone['cols'] = array();
     $clone->cols($this->native_col);
     // for all sub-eagers, if they are joining to the top-level fetch,
     // make sure they are join_only ... otherwise, they'll add columns,
     // which will mess up our cols() from earlier.
     foreach ($clone['eager'] as $sub_eager) {
         if ($sub_eager['join_flag']) {
             $sub_eager['join_only'] = true;
         }
     }
     // don't waste time ordering the results
     $clone['order'] = false;
     // build a select and get it as a string
     $select = $this->_native_model->newSelect($clone);
     $string = $select->__toString();
     $join = array('type' => "inner", 'name' => "({$string}) AS {$fetch['alias']}", 'cond' => "{$fetch['alias']}.{$this->native_col} = {$col}", 'cols' => null);
     // done!
     return $join;
 }
Example #3
0
 /**
  * 
  * Modifies the native fetch with an eager join so that the foreign table
  * is joined properly.
  * 
  * @param Solar_Sql_Model_Params_Eager $eager The eager params.
  * 
  * @param Solar_Sql_Model_Params_Fetch $fetch The native fetch params.
  * 
  * @return void
  * 
  * @see modEagerFetch()
  * 
  */
 protected function _modEagerFetch($eager, $fetch)
 {
     $join = array('type' => $eager['join_type'], 'name' => "{$this->foreign_table} AS {$eager['alias']}", 'cond' => array(), 'cols' => null);
     // primary-key join condition on foreign table
     $join['cond'][] = "{$fetch['alias']}.{$this->native_col} = " . "{$eager['alias']}.{$this->foreign_col}";
     // foreign and eager conditions
     $join['cond'] = array_merge($join['cond'], $this->getForeignConditions($eager['alias']), (array) $eager['conditions']);
     // done!
     $fetch->join($join);
     // always DISTINCT so we don't get multiple duplicate native rows
     $fetch->distinct(true);
 }
Example #4
0
 /**
  * 
  * Modifies the native fetch with eager joins so that the through table
  * and the foreign table are joined properly.
  * 
  * @param Solar_Sql_Model_Params_Eager $eager The eager params.
  * 
  * @param Solar_Sql_Model_Params_Fetch $fetch The native fetch params.
  * 
  * @return void
  * 
  */
 protected function _modEagerFetchJoin($eager, $fetch)
 {
     // first, join the native table to the through table
     $join = array('type' => 'inner', 'name' => "{$this->through_table} AS {$this->through_alias}", 'cond' => "{$fetch['alias']}.{$this->native_col} = " . "{$this->through_alias}.{$this->through_native_col}", 'cols' => null);
     $fetch->join($join);
     // then join to the through table to the foreign table
     $join = array('type' => $eager['join_type'], 'name' => "{$this->foreign_table} AS {$eager['alias']}", 'cond' => "{$eager['alias']}.{$this->foreign_col} = " . "{$this->through_alias}.{$this->through_foreign_col}", 'cols' => null);
     // extra conditions for the parent fetch
     if ($eager['join_cond']) {
         // what type of join?
         if ($join['type'] == 'left') {
             // convert the eager conditions to a WHERE clause
             foreach ((array) $eager['join_cond'] as $cond => $val) {
                 $fetch->where($cond, $val);
             }
         } else {
             // merge join conditions
             $join['cond'] = array_merge((array) $join['cond'], (array) $eager['join_cond']);
         }
     }
     // done!
     $fetch->join($join);
 }
Example #5
0
 /**
  * 
  * Modifies a params object **in place** to add joins for a tag list.
  * 
  * The methodology is taken and modified from
  * <http://forge.mysql.com/wiki/TagSchema#Items_Having_All_of_A_Set_of_Tags>.
  * 
  * @param Solar_Sql_Model_Params_Fetch $params The fetch params.
  * 
  * @param array $tags A list of unique tags.
  * 
  * @return void
  * 
  */
 protected function _modParamsJoinTags(Solar_Sql_Model_Params_Fetch $params, $tags)
 {
     // normalize the tag list
     $tags = $this->_fixTagList($tags);
     // if no tags, no need to modify
     if (!$tags) {
         return;
     }
     // since this model uses single-table inheritance, we need the model
     // alias, not just the table name.
     $alias = $this->_model_name;
     // for each tag, add a join to tags and taggings, chaining each
     // subsequent join to the previous one.
     // the first tag join-pair is special; we connect it to the nodes
     // table directly, since we don't have a previous join to chain from.
     $params->join(array('type' => "inner", 'name' => "taggings AS taggings1", 'cond' => "taggings1.node_id = {$alias}.id"));
     $params->join(array('type' => "inner", 'name' => "tags AS tags1", 'cond' => "taggings1.tag_id = tags1.id"));
     // take the first tag off the top of the list
     $val = array_shift($tags);
     $params->where("tags1.name = ?", $val);
     // now deal with all remaining tags, chaining each current join to the
     // previous one.
     foreach ($tags as $key => $val) {
         $curr = $key + 2;
         // because keys are zero-based, and we already shifted one
         $prev = $key + 1;
         // the "through" table
         $params->join(array('type' => "inner", 'name' => "taggings AS taggings{$curr}", 'cond' => "taggings{$curr}.node_id = taggings{$prev}.node_id"));
         // the "tags" table
         $params->join(array('type' => "inner", 'name' => "tags AS tags{$curr}", 'cond' => "taggings{$curr}.tag_id = tags{$curr}.id"));
         // the WHERE condition for the tag name
         $params->where("tags{$curr}.name = ?", $val);
     }
 }
Example #6
0
 /**
  * 
  * Modifies the native fetch params with eager joins so that the through 
  * table and the foreign table are joined properly.
  * 
  * @param Solar_Sql_Model_Params_Eager $eager The eager params.
  * 
  * @param Solar_Sql_Model_Params_Fetch $fetch The native fetch params.
  * 
  * @return void
  * 
  * @see modEagerFetch()
  * 
  */
 protected function _modEagerFetch($eager, $fetch)
 {
     // first, join the native table to the through table
     $thru = array('type' => strtolower($this->through_join_type), 'name' => "{$this->through_table} AS {$this->through_alias}", 'cond' => array(), 'cols' => null);
     $thru['cond'][] = "{$fetch['alias']}.{$this->native_col} = " . "{$this->through_alias}.{$this->through_native_col}";
     $thru['cond'] = array_merge($thru['cond'], $this->through_conditions);
     // keep for countPages() calls?
     if ($thru['type'] != 'left') {
         $thru['keep'] = true;
     } else {
         $thru['keep'] = false;
     }
     $fetch->join($thru);
     // now join to the through table to the foreign table
     $join = array('type' => strtolower($eager['join_type']), 'name' => "{$this->foreign_table} AS {$eager['alias']}", 'cond' => array(), 'cols' => null);
     $join['cond'][] = "{$eager['alias']}.{$this->foreign_col} = " . "{$this->through_alias}.{$this->through_foreign_col}";
     // foreign and eager conditions
     $join['cond'] = array_merge($join['cond'], $this->getForeignConditions($eager['alias']), (array) $eager['conditions']);
     // keep for countPages() calls only if we kept "through", and the
     // foreign join is not a left join
     if ($thru['keep'] && $join['type'] != 'left') {
         $join['keep'] = true;
     } else {
         $join['keep'] = false;
     }
     // done!
     $fetch->join($join);
     // always DISTINCT so we don't get multiple duplicate native rows
     $fetch->distinct(true);
 }
Example #7
0
 /**
  * 
  * Modifies the native fetch with an eager join so that the foreign table
  * is joined properly.
  * 
  * @param Solar_Sql_Model_Params_Eager $eager The eager params.
  * 
  * @param Solar_Sql_Model_Params_Fetch $fetch The native fetch params.
  * 
  * @return void
  * 
  */
 protected function _modEagerFetchJoin($eager, $fetch)
 {
     $join = array('type' => $eager['join_type'], 'name' => "{$this->foreign_table} AS {$eager['alias']}", 'cond' => array(), 'cols' => null);
     // primary-key join condition on foreign table
     $join['cond'][] = "{$fetch['alias']}.{$this->native_col} = " . "{$eager['alias']}.{$this->foreign_col}";
     // extra conditions for the parent fetch
     if ($eager['join_cond']) {
         // what type of join?
         if ($join['type'] == 'left') {
             // convert the eager conditions to a WHERE clause
             foreach ((array) $eager['join_cond'] as $cond => $val) {
                 $fetch->where($cond, $val);
             }
         } else {
             // merge join conditions
             $join['cond'] = array_merge($join['cond'], (array) $eager['join_cond']);
         }
     }
     // done!
     $fetch->join($join);
     // always DISTINCT so we don't get multiple duplicate native rows
     $fetch->distinct(true);
 }
Example #8
0
 /**
  * 
  * Gets the key for a cache entry based on fetch parameters for a select.
  * 
  * The entry is keyed under `$prefix/model/$model_name/data/$version/$hash`,
  * where $hash is an MD5 hash of the serialized parameters.
  * 
  * If the params include a `cache_key` entry, that value is used instead
  * of $hash.
  * 
  * @param array $fetch The fetch parameters for a select.
  * 
  * @return string The versioned cache entry key.
  * 
  */
 public function entry(Solar_Sql_Model_Params_Fetch $fetch)
 {
     $version = (int) $this->_fetchVersion();
     if ($fetch['cache_key']) {
         $key = $fetch['cache_key'];
     } else {
         $array = $fetch->toArray();
         unset($array['cache']);
         unset($array['cache_key']);
         unset($array['count_pages']);
         $key = hash('md5', serialize($array));
     }
     $key = $this->_prefix . "/model" . "/{$this->_model->model_name}" . "/data" . "/{$version}" . "/{$key}";
     return $key;
 }
 /**
  * 
  * Modifies the native fetch params with eager joins so that the through 
  * table and the foreign table are joined properly.
  * 
  * @param Solar_Sql_Model_Params_Eager $eager The eager params.
  * 
  * @param Solar_Sql_Model_Params_Fetch $fetch The native fetch params.
  * 
  * @return void
  * 
  * @see modEagerFetch()
  * 
  */
 protected function _modEagerFetch($eager, $fetch)
 {
     // first, join the native table to the through table
     $thru = array('type' => 'left', 'name' => "{$this->through_table} AS {$this->through_alias}", 'cond' => array(), 'cols' => null);
     $thru['cond'][] = "{$fetch['alias']}.{$this->native_col} = " . "{$this->through_alias}.{$this->through_native_col}";
     $thru['cond'] = array_merge($thru['cond'], $this->through_conditions);
     $fetch->join($thru);
     // now join to the through table to the foreign table
     $join = array('type' => $eager['join_type'], 'name' => "{$this->foreign_table} AS {$eager['alias']}", 'cond' => array(), 'cols' => null);
     $join['cond'][] = "{$eager['alias']}.{$this->foreign_col} = " . "{$this->through_alias}.{$this->through_foreign_col}";
     // foreign and eager conditions
     $join['cond'] = array_merge($join['cond'], $this->getForeignConditions($eager['alias']), (array) $eager['conditions']);
     // done!
     $fetch->join($join);
     // always DISTINCT so we don't get multiple duplicate native rows
     $fetch->distinct(true);
 }
Example #10
0
 /**
  * 
  * Fixes the native fetch params based on the settings for this related.
  * 
  * @param Solar_Sql_Model_Params_Fetch $fetch The native fetch settings.
  * 
  * @return void
  * 
  */
 protected function _fixFetchParams($fetch)
 {
     if (!$fetch['alias']) {
         $fetch->alias($this->native_alias);
     }
 }
Example #11
0
 /**
  * 
  * Modifies the native fetch with an eager join so that columns are
  * selected from the foreign table.
  * 
  * @param Solar_Sql_Model_Params_Eager $eager The eager params.
  * 
  * @param Solar_Sql_Model_Params_Fetch $fetch The native fetch params.
  * 
  * @return void
  * 
  */
 protected function _modEagerFetchJoin($eager, $fetch)
 {
     // the basic join array
     $join = array('type' => strtolower($eager['join_type']), 'name' => "{$this->foreign_table} AS {$eager['alias']}", 'cond' => array(), 'cols' => null);
     // standard to-one condition (works for both has-one and belongs-to)
     $join['cond'][] = "{$fetch['alias']}.{$this->native_col} = " . "{$eager['alias']}.{$this->foreign_col}";
     // extra conditions for the parent fetch
     if ($eager['join_cond']) {
         // what type of join?
         if ($join['type'] == 'left') {
             // convert the eager conditions to a WHERE clause
             foreach ((array) $eager['join_cond'] as $cond => $val) {
                 $fetch->where($cond, $val);
             }
         } else {
             // merge join conditions
             $join['cond'] = array_merge($join['cond'], (array) $eager['join_cond']);
         }
     }
     // what columns to fetch?
     if (!$eager['cols']) {
         $cols = null;
     } else {
         $cols = array();
         foreach ($eager['cols'] as $col) {
             $cols[] = "{$col} AS {$eager['cols_prefix']}__{$col}";
         }
     }
     // add the columns
     $join['cols'] = $cols;
     // add the join to the parent fetch
     $fetch->join($join);
 }