|
@@ -157,11 +157,14 @@
|
|
|
public static function table_name(){
|
|
|
if(is_null(static::$table_name)){
|
|
|
$name = get_called_class();
|
|
|
- return substr($name, strrpos($name, '\\') + 1);
|
|
|
+ static::$table_name = substr($name, strrpos($name, '\\') + 1);
|
|
|
}
|
|
|
return static::$table_name;
|
|
|
}
|
|
|
public static function table(){
|
|
|
+ if(is_null(static::$table)){
|
|
|
+ static::$table = self::$pdo->table(static::table_name());
|
|
|
+ }
|
|
|
return static::$table;
|
|
|
}
|
|
|
public static function primary_key(){
|
|
@@ -179,7 +182,6 @@
|
|
|
}
|
|
|
if($pdo instanceof PDO){
|
|
|
self::$pdo = $pdo;
|
|
|
- self::$table = $pdo->table(static::table_name());
|
|
|
// @todo handle updating live instances
|
|
|
}else{
|
|
|
throw new \Exception("Invalid argument. Must pass a DSN string or a PDO instance");
|
|
@@ -233,7 +235,7 @@
|
|
|
return !is_null(self::cached_instance($id));
|
|
|
}
|
|
|
public static function delete(int $id){
|
|
|
- return self::$table->delete([
|
|
|
+ return static::table()->delete([
|
|
|
static::primary_key() => $id
|
|
|
]) > 0;
|
|
|
}
|
|
@@ -243,27 +245,14 @@
|
|
|
array_walk(self::$instances[$name], $fn);
|
|
|
}
|
|
|
}
|
|
|
- public static function each_where(callable $fn, array $filter = null, int $start = null, int $amount = null){
|
|
|
- $limit = '';
|
|
|
- if(!is_null($start) && !is_null($amount)){
|
|
|
- $limit .= " limit {$start}, {$amount}";
|
|
|
- }
|
|
|
- $query = self::query(
|
|
|
- "select ".static::primary_key().' id '.
|
|
|
- "from ".static::table_name().' '.
|
|
|
- self::$table->stringFilter($filter).
|
|
|
- $limit
|
|
|
- );
|
|
|
- foreach($query->fetchAll() as $row){
|
|
|
+ public static function each(callable $fn, array $filter = null, int $start = null, int $amount = null){
|
|
|
+ static::table()->each(function($row) use($fn){
|
|
|
$fn(self::instance((int)$row['id']));
|
|
|
- }
|
|
|
- }
|
|
|
- public static function each(callable $fn, int $start = null, int $amount = null){
|
|
|
- self::each_where($fn, null, $start, $amount);
|
|
|
+ }, [static::primary_key()], $filter, $start, $amount);
|
|
|
}
|
|
|
public static function fetch(array $filter = null, int $start = null, int $amount = null){
|
|
|
$results = [];
|
|
|
- self::each_where(function($item) use($results){
|
|
|
+ self::each(function($item) use($results){
|
|
|
$results[] = $item;
|
|
|
}, $filter, $start, $amount);
|
|
|
return $results;
|
|
@@ -279,6 +268,9 @@
|
|
|
return 0 === strpos($class, "Model\\");
|
|
|
});
|
|
|
}
|
|
|
+ public static function count(array $filter = null){
|
|
|
+ return static::table()->count($filter);
|
|
|
+ }
|
|
|
// Instance Api
|
|
|
public function values($values){
|
|
|
foreach($values as $key => $val){
|
|
@@ -317,11 +309,11 @@
|
|
|
}
|
|
|
}
|
|
|
if(!is_null($this->id) && !in_array(static::primary_key(), $this->_changed)){
|
|
|
- self::$table->update($data, [
|
|
|
+ static::table()->update($data, [
|
|
|
static::primary_key() => $this->id
|
|
|
]);
|
|
|
}else{
|
|
|
- self::$table->insert($data);
|
|
|
+ static::table()->insert($data);
|
|
|
$this->_data[static::primary_key()] = self::$pdo->lastInsertId();
|
|
|
}
|
|
|
foreach($this->_related as $related){
|
|
@@ -382,15 +374,15 @@
|
|
|
$aliases = self::$aliases[$this->name];
|
|
|
if(isset($aliases['belongs_to'][$name])){
|
|
|
$alias = $aliases['has_many'][$name];
|
|
|
- $class = "\\Models\\{$alias['model']}";
|
|
|
+ $class = "\\Model\\{$alias['model']}";
|
|
|
$this->_related[$name] = $class::fetch([$alias['foreign_key'] => $this->id])[0];
|
|
|
}elseif(isset($aliases['has_one'][$name])){
|
|
|
$alias = $aliases['has_many'][$name];
|
|
|
- $class = "\\Models\\{$alias['model']}";
|
|
|
+ $class = "\\Model\\{$alias['model']}";
|
|
|
$this->_related[$name] = $class::instance($this[$alias['foreign_key']]);
|
|
|
}elseif(isset($aliases['has_many'][$name])){
|
|
|
$alias = $aliases['has_many'][$name];
|
|
|
- $class = "\\Models\\{$alias['model']}";
|
|
|
+ $class = "\\Model\\{$alias['model']}";
|
|
|
$sql = "select ";
|
|
|
if($alias['through']){
|
|
|
$sql .= $class::table_name().$class::foreign_key_suffix()." id from {$alias['through']} ";
|