Example #1
0
 /**
  * Initialize a model binding it to a database table.
  *
  * @param string $table Name of the table
  * @param Database $db Database instance to use
  */
 public function init($table = null, Database $db = null)
 {
     # get the Database class instance
     $this->db = is_null($db) ? pew('db') : $db;
     $this->table = $table ?: $this->tableName();
     if (!$this->db->table_exists($this->table)) {
         throw new TableNotFoundException("Table {$this->table} for model " . get_class($this) . " not found.");
     }
     # some metadata about the table
     $primary_key = $this->db->get_pk($this->table);
     $columns = $this->db->get_cols($this->table);
     $this->tableData['name'] = $this->table;
     $this->tableData['primary_key'] = $primary_key;
     $this->tableData['columns'] = $columns;
     $this->tableData['column_names'] = array_combine($columns, array_fill(0, count($columns), null));
 }