Skip to content

Commit

Permalink
Models
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Feb 13, 2017
1 parent 83577ee commit 8eee94b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
62 changes: 40 additions & 22 deletions lib/object.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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';

Expand All @@ -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);
}
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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')));
}
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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']]);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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'));
}

Expand All @@ -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]);
}
}
Expand Down Expand Up @@ -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();
Expand Down
94 changes: 89 additions & 5 deletions lib/octalia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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', []);
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -633,19 +654,24 @@ 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'
]
)) {
$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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 8eee94b

Please sign in to comment.