Beispiel #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);
 }
 function __construct($source, $dest, $options = null)
 {
     $this->source_class = get_class($source);
     if (isset($options['class_name'])) {
         $this->dest_class = $options['class_name'];
     } else {
         $this->dest_class = ActiveRecordInflector::classify($dest);
     }
     if (isset($options['foreign_key'])) {
         $this->foreign_key = $options['foreign_key'];
     } else {
         $this->foreign_key = ActiveRecordInflector::foreign_key($this->source_class);
     }
     $this->options = $options;
 }
 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);
 }
Beispiel #4
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);
 }
Beispiel #5
0
 function __construct($source, $dest, $options = null)
 {
     $this->source_class = get_class($source);
     $this->dest_class = ActiveRecordInflector::classify($dest);
     $this->options = $options;
 }
foreach ($table_s as $table) {
    $tables[] = $table;
}
/* end hack for PDO */
foreach ($tables as $table_row) {
    $table_name = current($table_row);
    $table_vanity_name = $table_name;
    if (AR_PREFIX && AR_PREFIX != '') {
        $table_vanity_name = preg_replace('/^' . AR_PREFIX . '/', '', $table_name);
    }
    if (is_array($AR_TABLES)) {
        if (!in_array($table_vanity_name, $AR_TABLES)) {
            continue;
        }
    }
    $class_name = ActiveRecordInflector::classify($table_vanity_name);
    $columns_q = ActiveRecord::query("DESC {$table_name}");
    $columns = array();
    foreach ($columns_q as $column_row) {
        $columns[] = "'" . $column_row['Field'] . "'";
        if ($column_row['Key'] == 'PRI') {
            $primary_key = $column_row['Field'];
        }
    }
    if (!file_exists($stub_models_dir . $class_name . ".php")) {
        $gen_file = $stub_template;
        $gen_file = preg_replace('/{\\$class_name}/', $class_name, $gen_file);
        file_put_contents($stub_models_dir . $class_name . ".php", $gen_file);
    }
    $gen_file = $template;
    $gen_file = preg_replace('/{\\$ar_dir}/', $this_dir, $gen_file);