/** * Facade, implementation de la fonction magic __callStatic de PHP * * @param string $method Le nom de la method a appelé * @param array $args Les arguments a passé à la fonction * @throws ModelException * @return \Bow\Database\Table */ public static function __callStatic($method, $args) { $scope = 'scope' . ucfirst($method); $table = Database::table(Str::lower(static::$table)); /** * Lancement de l'execution des fonctions aliase définir dans les classe * héritant de la classe Model. * * Les classes definir définir avec la montion scope. */ if (method_exists($child = new static(), $scope)) { if (method_exists($table, $method)) { throw new ModelException($method . ' ne peut pas être utiliser comme fonction d\'aliase.', E_ERROR); } return call_user_func_array([$child, $scope], $args); } /** * Lancement de l'execution des fonctions liée a l'instance de la classe Table */ if (method_exists($table, $method)) { $table = call_user_func_array([$table, $method], $args); return static::carbornize($table, $method, $child); } throw new ModelException('methode ' . $method . ' n\'est définie.', E_ERROR); }
/** * Change le nom d'un table. * * @param string $newTableName Le nom de la nouveau table */ public function change($newTableName) { if (Database::statement("RENAME TABLE " . $this->tableName . " TO " . $newTableName)) { echo "[0;32mTabe renamed.[00m\n"; } else { echo "[0;31m Cannot rename table.[00m\n"; } }
/** * valider une transaction */ function commit() { Database::commit(); }
/** * Join avec une autre table * * @param string $table * @param mixed $foreign_key * @return self */ public function merge($table, $foreign_key = null) { $foreign = 'id'; if ($foreign_key == null) { if ($this->foreign !== null) { $foreign = $this->foreign; } } $this->data->{$table} = Database::table($table)->where($foreign, $this->id)->get(); $this->mergeTableName = $table; return $this; }
/** * fillTable, remplir un table pour permet le developpement. * * @param string|array $table [optional] * @param int $n * @param array $desciption [ * "column" => [ * "field" => "name", * "type": "int|longint|bigint|mediumint|smallint|tinyint", * "auto" => false|true * ] * @return mixed */ public static function fillTable($table = null, $n = 1, $desciption = []) { if (is_int($table)) { $n = $table; $table = null; } if (!is_string($table)) { $table = static::$table; } $database = Database::table($table); if (static::$data === null) { static::$data = $desciption; } $r = 0; for ($i = 0; $i < $n; $i++) { $data = []; foreach (static::$data as $column) { if (in_array($column['type'], ['int', 'longint', 'bigint', 'mediumint', 'smallint', 'tinyint'])) { if ($column['auto']) { $value = null; } else { $value = Filler::number(); } } else { if (in_array($column['type'], ['date', 'datetime'])) { $value = Filler::date(); } else { if (in_array($column['type'], ['double', 'float'])) { $value = Filler::float(); } else { if ($column['type'] == 'timestamp') { $value = time(); } else { if ($column['type'] == 'enum') { $value = $column['default']; } else { if (preg_match('/text$/', $column['type'])) { $value = Str::slice(Filler::string(), 0, 1000); } else { $value = Str::slice(Filler::string(), 0, rand(1, $column['size'])); } } } } } } $data[$column['field']] = $value; } $r += $database->insert($data); } return $r; }