diff --git a/lib/lib.php b/lib/lib.php index b6e8f44..4c14511 100755 --- a/lib/lib.php +++ b/lib/lib.php @@ -3724,7 +3724,7 @@ function di() return new Utils; } - function engine($database = 'core', $table = 'core') + function engine($database = 'core', $table = 'core', $driver = 'ldb') { $engine = Config::get('octalia.engine', 'ldb'); diff --git a/lib/object.php b/lib/object.php index 51a8fb9..99f7bbb 100644 --- a/lib/object.php +++ b/lib/object.php @@ -86,7 +86,7 @@ function checkAndSave(callable $cb) public function touch($model) { - return odb($this->db(), $model, $this->driver())->findOrFail((int) $this->data[$model . '_id'])->now(); + return engine($this->db(), $model, $this->driver())->findOrFail((int) $this->data[$model . '_id'])->now(); } public function hasModel() @@ -137,6 +137,20 @@ public function collection() public function __call($m, $a) { + if ('actualValue' == $m) { + $default = null; + + if (count($a) == 2) { + $default = $a[1]; + } + + if ($this->exists()) { + return isAke($this->initial, current($a), $default); + } else { + return $default; + } + } + if ('array' == $m) { return $this->data; } @@ -265,12 +279,12 @@ public function __call($m, $a) } } - return odb($this->db(), $fkParent, $this->driver())->where([$fk, '=', (int) $this->get('id')]); + return engine($this->db(), $fkParent, $this->driver())->where([$fk, '=', (int) $this->get('id')]); } else { $id = isAke($this->data, $fktable . '_id', 'octodummy'); if (is_numeric($id)) { - return odb($this->db(), $fktable, $this->driver())->find((int) $id, $o); + return engine($this->db(), $fktable, $this->driver())->find((int) $id, $o); } else { $fk = $this->table() . '_id'; @@ -282,7 +296,7 @@ public function __call($m, $a) } } - return odb($this->db(), $fktable, $this->driver())->where([$fk, '=', (int) $this->get('id')])->first($model); + return engine($this->db(), $fktable, $this->driver())->where([$fk, '=', (int) $this->get('id')])->first($model); } } } @@ -303,7 +317,7 @@ public function __call($m, $a) public function fresh() { if ($this->hasModel() && $this->exists()) { - return odb($this->db(), $this->table(), $this->driver())->find((int) $this->data['id']); + return engine($this->db(), $this->table(), $this->driver())->find((int) $this->data['id']); } return $this; @@ -415,7 +429,7 @@ public function get($k, $d = null) $fk = $this->table() . '_id'; $fkParent = substr($k, 0, -1); - $query = odb($this->db(), $fkParent, $this->driver())->where([$fk, '=', (int) $this->get('id')]); + $query = engine($this->db(), $fkParent, $this->driver())->where([$fk, '=', (int) $this->get('id')]); if ($query->count() > 0) { return $query; @@ -424,11 +438,11 @@ public function get($k, $d = null) $id = isAke($this->data, $k . '_id', 'octodummy'); if (is_numeric($id)) { - return odb($this->db(), $k, $this->driver())->row((int) $this->get($k . '_id')); + return engine($this->db(), $k, $this->driver())->row((int) $this->get($k . '_id')); } else { $fk = $this->table() . '_id'; - $query = odb($this->db(), $k, $this->driver())->where([$fk, '=', (int) $this->get('id')]); + $query = engine($this->db(), $k, $this->driver())->where([$fk, '=', (int) $this->get('id')]); if ($query->count() > 0) { return $query->first(); @@ -480,14 +494,14 @@ public function has($k) } } - $query = odb($this->db(), $fkParent, $this->driver())->where([$fk, '=', (int) $this->get('id')]); + $query = engine($this->db(), $fkParent, $this->driver())->where([$fk, '=', (int) $this->get('id')]); return $query->count() > 0; } else { $id = $this->get($k . '_id', 'octodummy'); if (is_numeric($id)) { - return !empty(odb($this->db(), $k, $this->driver())->row((int) $this->get($k . '_id'))); + return !empty(engine($this->db(), $k, $this->driver())->row((int) $this->get($k . '_id'))); } } } @@ -526,7 +540,7 @@ public function __unset($k) } } - $rows = odb($this->db(), $fkParent, $this->driver())->where([$fk, '=', (int) $this->get('id')]); + $rows = engine($this->db(), $fkParent, $this->driver())->where([$fk, '=', (int) $this->get('id')]); if ($rows->count() > 0) { $rows->delete(); @@ -535,7 +549,7 @@ public function __unset($k) $id = $this->get($k . '_id', 'octodummy'); if (is_numeric($id)) { - $row = odb($this->db(), $k, $this->driver())->find((int) $this->get($k . '_id')); + $row = engine($this->db(), $k, $this->driver())->find((int) $this->get($k . '_id')); if ($row) { $row->delete(); @@ -587,7 +601,9 @@ public function take($fk) exception('model', 'id must be defined to use take.'); } - $db = fnmatch('*s', $fk) ? odb($this->db(), substr($fk, 0, -1), $this->driver()) : odb($this->db(), $fk, $this->driver()); + $db = fnmatch('*s', $fk) + ? engine($this->db(), substr($fk, 0, -1), $this->driver()) + : engine($this->db(), $fk, $this->driver()); return $db->where([$this->table() . '_id', '=', (int) $this->data['id']]); } @@ -708,7 +724,9 @@ public function adjust($field, $cb = null) public function validate() { if ($this->hasModel() && $this->exists()) { - $check = odb($this->db(), $this->table(), $this->driver())->validator()->check($this->toArray()); + $check = engine($this->db(), $this->table(), $this->driver()) + ->validator() + ->check($this->toArray()); if ($check) { $this->save(); @@ -726,7 +744,7 @@ public function polymorph(Object $polymorph = null) { if ($this->hasModel() && $this->exists()) { if (is_null($polymorph)) { - return odb($this->db(), $this->polymorph_type, $this->driver())->find((int) $this->polymorph_id); + return engine($this->db(), $this->polymorph_type, $this->driver())->find((int) $this->polymorph_id); } $this->data['polymorph_type'] = $polymorph->table(); @@ -739,7 +757,7 @@ public function polymorph(Object $polymorph = null) public function polymorphs($parent) { if ($this->hasModel() && $this->exists()) { - return odb($this->db(), $parent, $this->driver()) + return engine($this->db(), $parent, $this->driver()) ->where('polymorph_type', $this->table()) ->where('polymorph_id', (int) $this->id); } @@ -754,9 +772,9 @@ public function relationship(Octalia $model, $many = true) $idFk = $fk . '_id'; if (isset($this->data[$idFk]) && is_numeric($this->data[$idFk])) { - return odb($this->db(), $fk, $this->driver())->find((int) $this->data[$idFk]); + return engine($this->db(), $fk, $this->driver())->find((int) $this->data[$idFk]); } else { - $query = odb($this->db(), $fk, $this->driver())->where($this->table() . '_id', (int) $this->get('id')); + $query = engine($this->db(), $fk, $this->driver())->where($this->table() . '_id', (int) $this->get('id')); return $many ? $query : $query->first(true); } @@ -792,7 +810,7 @@ public function manyToMany(Octalia $model) sort($tables); $pivot = implode('', $tables); - return odb($this->db(), $pivot, $this->driver()) + return engine($this->db(), $pivot, $this->driver()) ->where($this->table() . '_id', (int) $this->get('id')); } @@ -812,10 +830,10 @@ public function pivots(Octalia $model) } if (empty($ids)) { - return odb($this->db(), $model->table, $this->driver()) + return engine($this->db(), $model->table, $this->driver()) ->where(['id', '<', 0]); } else { - return odb($this->db(), $model->table, $this->driver()) + return engine($this->db(), $model->table, $this->driver()) ->where(['id', 'IN', $ids]); } } @@ -862,7 +880,7 @@ public function detach(Object $model) sort($tables); $pivot = implode('', $tables); - return odb($this->db(), $pivot, $this->driver()) + return engine($this->db(), $pivot, $this->driver()) ->where($this->table() . '_id', (int) $this->get('id')) ->where($model->table() . '_id', (int) $model->id) ->delete(); diff --git a/lib/octalia.php b/lib/octalia.php index 070cdf2..b0d60bf 100644 --- a/lib/octalia.php +++ b/lib/octalia.php @@ -287,6 +287,25 @@ public function push($row) return $this->add($row); } + public function createMany(array $rows = [], $return = false) + { + if ($return) { + $collection = []; + } + + foreach ($rows as $data) { + $new = $this->model($data); + + if ($return) { + $collection[] = $new; + } else { + $new->save(); + } + } + + return $return ? $collection : $this; + } + public function create(array $data = []) { return $this->model($data); @@ -466,7 +485,7 @@ public function model($row) $row = treatCast($row); if (file_exists($file)) { - $cbs = require $file; + $cbs = require_once $file; $fns = isAke($cbs, 'scopes', []); $hooks = isAke($cbs, 'hooks', []); @@ -520,12 +539,14 @@ public function model($row) $fs = '[' . "\n\t\t\t"; foreach ($fields as $field) { - if ($field == 'age' || $field == 'number' || $field == 'quantity' || $field == 'id' || $field == 'created_at' || $field == 'updated_at' || $field == 'deleted_at' || fnmatch('*_id', $field)) { + if ($field == 'year' || $field == 'age' || $field == 'number' || $field == 'quantity' || $field == 'id' || $field == 'created_at' || $field == 'updated_at' || $field == 'deleted_at' || $field == 'created_by' || $field == 'updated_by' || $field == 'deleted_by' || fnmatch('*_id', $field)) { $type = 'integer'; } elseif (in_array( $field, [ + 'duration', 'price', 'size', + 'length', 'width', 'height', 'depth' @@ -633,11 +654,16 @@ public function model($row) $val = 'token()'; } elseif (in_array( $field, [ + 'created_by', + 'updated_by', + 'deleted_by', + 'year', 'age', 'price', 'size', 'width', 'height', + 'length', 'depth', 'quantity', 'number' @@ -645,7 +671,7 @@ public function model($row) )) { $val = '$faker->numberBetween(15, 85)'; } elseif (fnmatch('*_id', $field)) { - $val = 'engine("' . $this->db . '", "' . str_replace('_id', '', $field) . '")->createFake()->id'; + $val = 'em("' . $this->db . '", "' . str_replace('_id', '', $field) . '")->createFake()->id'; } if ($n < count($fields) - 1) { @@ -1036,9 +1062,32 @@ public function __call($m, $a) if (fnmatch('findBy*', $m) && strlen($m) > 6) { $field = callField($m, 'findBy'); - $value = array_shift($a); - return $this->where([$field, '=', $value]); + $op = '='; + + if (count($a) == 2) { + $op = array_shift($a); + $value = array_shift($a); + } else { + $value = array_shift($a); + } + + return $this->where([$field, $op, $value]); + } + + if (fnmatch('where*', $m) && strlen($m) > 5) { + $field = callField($m, 'where'); + + $op = '='; + + if (count($a) == 2) { + $op = array_shift($a); + $value = array_shift($a); + } else { + $value = array_shift($a); + } + + return $this->where([$field, $op, $value]); } if (fnmatch('by*', $m) && strlen($m) > 2) { @@ -1096,6 +1145,31 @@ public function __call($m, $a) return $this->lastBy($field, $value, $model); } + if (count($a) == 1) { + $o = array_shift($a); + + if ($o instanceof Object) { + $fk = Strings::uncamelize($m) . '_id'; + + return $this->where([$fk, '=', (int) $o->id]); + } + } + + $file = path('models') . '/' . $this->db . '/' . $this->table . '.php'; + + if (file_exists($file)) { + $cbs = require_once $file; + + $scopes = isAke($cbs, 'scopes', []); + $cb = isAke($scopes, $m, null); + + if ($cb && is_callable($cb)) { + $args = array_merge([$this], $a); + + return call_user_func_array($cb, $args); + } + } + $data = $this->data(); return call_user_func_array([coll($data), $m], $a); @@ -1869,6 +1943,16 @@ public function notIn($field, array $values) return $this->where($field, 'not in', $values); } + public function WhereIn($field, array $values) + { + return $this->where($field, 'in', $values); + } + + public function whereNotIn($field, array $values) + { + return $this->where($field, 'not in', $values); + } + public function rand($default = null) { $items = (array) $this->getIterator();