Пример #1
0
 function join()
 {
     $dest_table = ActiveRecordInflector::tableize($this->dest_class);
     $source_table = ActiveRecordInflector::tableize($this->source_class);
     $dest_inst = new $this->dest_class();
     $columns = $dest_inst->get_columns();
     $join = "LEFT OUTER JOIN {$dest_table} ON " . "{$source_table}.{$this->foreign_key} = {$dest_table}." . $dest_inst->get_primary_key();
     return array(array($dest_table => $columns), $join);
 }
Пример #2
0
 function generate_find_query($class_name, $id, $options = null)
 {
     //$dbh =& $this->get_dbh();
     $item = new $class_name();
     $options = self::decode_if_json($options);
     /* first sanitize what we can */
     if (is_array($id)) {
         foreach ($id as $k => $v) {
             $id[$k] = self::quote($v);
         }
     } elseif ($id != 'all' && $id != 'first') {
         $id = self::quote($id);
     }
     /* regex for limit, order, group */
     $regex = '/^[A-Za-z0-9\\-_ ,\\(\\)]+$/';
     if (!isset($options['limit']) || !preg_match($regex, $options['limit'])) {
         $options['limit'] = '';
     }
     if (!isset($options['order']) || !preg_match($regex, $options['order'])) {
         $options['order'] = '';
     }
     if (!isset($options['group']) || !preg_match($regex, $options['group'])) {
         $options['group'] = '';
     }
     if (!isset($options['offset']) || !is_numeric($options['offset'])) {
         $options['offset'] = '';
     }
     $select = '*';
     if (is_array($id)) {
         $where = "{$item->primary_key} IN (" . implode(",", $id) . ")";
     } elseif ($id == 'first') {
         $limit = '1';
     } elseif ($id != 'all') {
         $where = "{$item->table_name}.{$item->primary_key} = {$id}";
     }
     if (isset($options['conditions'])) {
         $cond = self::convert_conditions_to_where($options['conditions']);
         $where = isset($where) && $where ? $where . " AND " . $cond : $cond;
     }
     if ($options['offset']) {
         $offset = $options['offset'];
     }
     if ($options['limit'] && !isset($limit)) {
         $limit = $options['limit'];
     }
     if (isset($options['select'])) {
         $select = $options['select'];
     }
     $joins = array();
     $tables_to_columns = array();
     $column_lookup = array();
     if (isset($options['include'])) {
         array_push($tables_to_columns, array(ActiveRecordInflector::tableize(get_class($item)) => $item->get_columns()));
         $includes = preg_split('/[\\s,]+/', $options['include']);
         // get join part of query from association and column names
         foreach ($includes as $include) {
             if (isset($item->associations[$include])) {
                 list($cols, $join) = $item->associations[$include]->join();
                 array_push($joins, $join);
                 array_push($tables_to_columns, $cols);
             }
         }
         // set the select variable so all column names are unique
         $selects = array();
         foreach ($tables_to_columns as $table_key => $columns) {
             foreach ($columns as $table => $cols) {
                 foreach ($cols as $key => $col) {
                     array_push($selects, "{$table}.`{$col}` AS t{$table_key}_r{$key}");
                     $column_lookup["t{$table_key}_r{$key}"]["table"] = $table;
                     $column_lookup["t{$table_key}_r{$key}"]["column"] = $col;
                 }
             }
         }
         $select = implode(", ", $selects);
     }
     // joins (?), include
     $query = "SELECT {$select} FROM {$item->table_name}";
     $query .= count($joins) > 0 ? " " . implode(" ", $joins) : "";
     $query .= isset($where) ? " WHERE {$where}" : "";
     $query .= $options['group'] ? " GROUP BY {$options['group']}" : "";
     $query .= $options['order'] ? " ORDER BY {$options['order']}" : "";
     $query .= isset($limit) && $limit ? " LIMIT {$limit}" : "";
     $query .= isset($offset) && $offset ? " OFFSET {$offset}" : "";
     return array('query' => $query, 'column_lookup' => $column_lookup);
 }
Пример #3
0
 function join()
 {
     $dest_table = ActiveRecordInflector::tableize($this->dest_class);
     $source_table = ActiveRecordInflector::tableize($this->source_class);
     $source_inst = new $this->source_class();
     $dest_inst = new $this->dest_class();
     $columns = $dest_inst->get_columns();
     if (!isset($this->options['through']) || !$this->options['through']) {
         $join = "LEFT OUTER JOIN {$dest_table} ON " . "{$dest_table}.{$this->foreign_key} = {$source_table}." . $source_inst->get_primary_key();
     } else {
         $join = "LEFT OUTER JOIN {$this->options['through']} ON " . "{$this->options['through']}.{$this->foreign_key} = {$source_table}." . $source_inst->get_primary_key() . " " . "LEFT OUTER JOIN {$dest_table} ON " . "{$dest_table}." . $dest_inst->get_primary_key() . " = {$this->options['through']}." . ActiveRecordInflector::foreign_key($this->dest_class);
     }
     return array(array($dest_table => $columns), $join);
 }