Exemplo n.º 1
0
 /**
  *
  *	This is used to narrow down results
  *
  *	@example only gather products from a specific category
  *			 $products = new Collection('tb_products', 'cat_id', $_GET['cat_id']);
  *
  *	@param string $table The name of the table this collection represents
  *	@param string $field The field to qualify which records are retrieved
  *	
  */
 public function get($modelType = null)
 {
     if (!is_null($modelType)) {
         $m = new $modelType();
         $this->table = $m->table;
     } else {
         if (!is_null($this->model)) {
             $m = new $this->model();
             $this->table = $m->table;
         }
     }
     $this->items = [];
     $this->db->select('*')->from($this->table);
     $q = $this->db->build_query();
     if (Model_Provider::has($q)) {
         $this->items = Model_Provider::get($q);
     } else {
         $this->items = $this->db->get();
         foreach ($this->items as $key => $item) {
             if (!is_null($modelType)) {
                 $model = new $modelType();
             } else {
                 if (!is_null($this->model)) {
                     $model = new $this->model();
                 } else {
                     $model = new Model($this->table, false);
                 }
             }
             $model->fill($item);
             $this->items[$key] = $model;
         }
         Model_Provider::set($q, $this->items);
     }
 }
Exemplo n.º 2
0
 public function belongsToMany($model, $join_table, $this_id = null, $that_id = null, $where = [])
 {
     $m = new $model();
     if (is_null($this_id)) {
         $this_id = strtolower(get_class($this)) . '_id';
     }
     if (is_null($that_id)) {
         $that_id = strtolower($model) . '_id';
     }
     $id = $this->primary_key;
     $fields = [];
     foreach ($m->fields as $field) {
         $fields[] = $m->table . '.' . $field;
     }
     $this->db->select(implode(', ', $fields))->from(implode(', ', [$join_table, $m->table, $this->table]))->where($m->table . '.' . $m->primary_key, $join_table . '.' . $that_id, false)->where($this->table . '.' . $this->primary_key, $join_table . '.' . $this_id, false)->where($this->table . '.' . $this->primary_key, $this->{$id})->where($where);
     $q = $this->db->build_query();
     if (Model_Provider::has($q)) {
         // echo "<div class='alert alert-success'>$q</div>";
         return Model_Provider::get($q);
     } else {
         $results = $this->db->get();
         $items = [];
         foreach ($results as $result) {
             $m = new $model();
             $m->fill($result);
             $items[] = $m;
         }
         Model_Provider::set($q, $items);
         return $items;
     }
 }