Exemplo n.º 1
0
 /**
  * Returns the table name. If the property Model::isPlural is not overridden, then the singular form of the classname is used.
  *
  * @return string
  */
 public function getTableName()
 {
     if (isset($this->table)) {
         return $this->table;
     }
     return TableHelper::getTableName($this);
 }
Exemplo n.º 2
0
 /**
  * Sets meta data for the object.
  *
  * @param $resource object An instance of p810\MySQL\Connection.
  * @param $table object|string An instance of p810\Model\Model or the table name as a string.
  * @param $data array Data returned by the query.
  * @return void
  */
 function __construct(Connection $resource, $table, $data)
 {
     $this->resource = $resource;
     $this->data = $data;
     if (is_object($table) && $table instanceof Model) {
         $this->table = $table->getTableName();
         $this->identifyBy($table->getPrimaryKey());
     } else {
         $this->table = TableHelper::getTableName($table);
         $primaryKey = TableHelper::getPrimaryKey($table);
         // Attempt to find the primary key so that most tables won't require a manual call to Row::identifyBy()
         if (array_key_exists($primaryKey, $data)) {
             $this->identifyBy($primaryKey);
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Calls a method belonging to Query and returns its result.
  *
  * @param $method string The name of the method to call.
  * @param $arguments array A variadic list of arguments.
  * @return bool|array
  */
 public function map()
 {
     if (!method_exists($this, $this->method)) {
         throw new \BadMethodCallException();
     }
     $table = $this->arguments[0];
     if (isset($this->arguments[1])) {
         $foreign_key = $this->arguments[1];
     } else {
         $foreign_key = null;
     }
     $table = Inflector::pluralize($table);
     if ($this->method == 'belongsToMany') {
         $bridge = $this->local . '_to_' . $table;
     } else {
         $bridge = null;
     }
     if (is_null($foreign_key)) {
         switch ($this->method) {
             case 'hasOne':
             case 'hasMany':
             case 'belongsToMany':
                 $foreign_key = TableHelper::getPrimaryKey($this->local);
                 break;
             case 'belongsToOne':
                 $foreign_key = TableHelper::getPrimaryKey($table);
                 break;
         }
     }
     $arguments = [$table, $foreign_key, $bridge];
     if (is_null($bridge)) {
         unset($arguments[2]);
     }
     $results = call_user_func_array([$this, $this->method], $arguments);
     $this->updateModel($results);
     return $results;
 }
Exemplo n.º 4
0
 /**
  * Maps a many-to-many relationship between two tables, through an intermediary.
  *
  * This method performs two queries. The first will find all rows in $intermediary where the
  * $key is equal to the ID contained in Relationship::$id. Then, the foreign table will be queried
  * for each matching row, and the results will be stored. Example:
  *
  * > select * from `roles_to_permissions` where `role_id` = 1;
  *
  * Then the loop, where :id is the ID of the result:
  *
  * > select * from `permissions` where `permission_id` = :id;
  *
  * @param $table string The name of the foreign table.
  * @param $key string The primary key of the local table.
  * @param $intermediary string The name of the intermediary table.
  * @return bool|array
  */
 protected function belongsToMany($table, $key, $intermediary)
 {
     $query = $this->resource->select($this->columns, $intermediary);
     $query->where($key, $this->id);
     $results = $query->execute();
     if (count($results) > 0) {
         $return = array();
         $primary_key = TableHelper::getPrimaryKey($table);
         foreach ($results as $result) {
             $query = $this->resource->select('*', $table);
             $query->where($primary_key, $result[$primary_key]);
             $result = $query->execute();
             $return[] = array_shift($result);
         }
         $results = $return;
     } else {
         $results = false;
     }
     return $results;
 }