Esempio n. 1
0
 /**
  * Constructs the model. To use ORM it is enough to
  * just create a model like this:
  * <code>
  * class App\Model\Fairy extends \PHPixie\ORM\Model { }
  * </code>
  * By default it will assume that the name of your table
  * is the plural form of the models' name, the PRIMARY KEY is id,
  * and will use the 'default' connection. This behaviour is easy to be
  * changed by overriding $table, $id and $db properties.
  *
  * @param \PHPixie\Pixie $pixie Pixie dependency container
  * @return void
  * @see $table
  * @see $id
  * @see $db
  */
 public function __construct($pixie)
 {
     $this->pixie = $pixie;
     $this->conn = $pixie->db->get($this->connection);
     $this->query = $this->conn->query('select');
     $this->model_name = strtolower(get_class($this));
     $this->model_name = str_ireplace($this->pixie->app_namespace . "Model\\", '', $this->model_name);
     if ($this->table == null) {
         $this->table = str_replace("\\", '_', $this->model_name);
         $this->table = $this->plural($this->table);
     }
     $this->query->table($this->table);
     foreach (array('belongs_to', 'has_one', 'has_many') as $rels) {
         $normalized = array();
         foreach ($this->{$rels} as $key => $rel) {
             if (!is_array($rel)) {
                 $key = $rel;
                 $rel = array();
             }
             $normalized[$key] = $rel;
             if (!isset($rel['model'])) {
                 $rel['model'] = $normalized[$key]['model'] = $rels == 'has_many' ? $this->singular($key) : $key;
             }
             $normalized[$key]['type'] = $rels;
             if (!isset($rel['key'])) {
                 $normalized[$key]['key'] = $this->model_key($rels != 'belongs_to' ? $this->model_name : $rel['model']);
             }
             if ($rels == 'has_many' && isset($rel['through'])) {
                 if (!isset($rel['foreign_key'])) {
                     $normalized[$key]['foreign_key'] = $this->model_key($rel['model']);
                 }
             }
             $normalized[$key]['name'] = $key;
         }
         $this->{$rels} = $normalized;
     }
 }