From 9193788a1ba9987fa5105e01802ab4bcf83fdf1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Plusquellec?= Date: Thu, 8 Jun 2017 11:47:53 +0200 Subject: [PATCH] corrections --- lib/octalia.php | 79 +++++++++++++++++++++++++++++++++++++---- lib/octaliaiterator.php | 4 +++ lib/testcase.php | 4 +-- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/lib/octalia.php b/lib/octalia.php index b093eb0..3f3a973 100644 --- a/lib/octalia.php +++ b/lib/octalia.php @@ -464,7 +464,7 @@ private function modify(array $data, $model = true) public function delete($id = null, $soft = false, $fire = true) { if (is_null($id)) { - return $this->deletes(); + return 0 < $this->count() ? $this->deletes() : $this; } $row = $this->row($id); @@ -574,6 +574,10 @@ public function rowAndDelete($id) public function model($row = []) { + if (is_null($row)) { + $row = []; + } + $row = is_object($row) ? $row->toArray() : $row; $class = Strings::camelize($this->db . '_' . $this->table . '_model'); @@ -1076,6 +1080,18 @@ public function __call($m, $a) ); } + if ($m == 'resetted') { + return new self($this->db, $this->table, $this->driver); + } + + if ($m == 'empty') { + $this->driver->set('rows', []); + + $this->age(microtime(true)); + + return $this->resetted(); + } + if ($m == 'or') { if (empty($this->query)) { exception('octalia', 'You must have at least one where clause before using the method or.'); @@ -1995,12 +2011,21 @@ public function deletes() { $deleted = 0; - foreach ($this->get() as $item) { - if (isset($item['id'])) { - $row = $this->find((int) $item['id']); + foreach ($this->get(false) as $item) {var_dump($item); + if ($item) { + $id = $item['id']; + + if (is_numeric($id)) { + $this->driver->delete($id); + + $rows = $this->driver->get('rows', []); + + unset($rows[$id]); + + $this->driver->set('rows', $rows); + + $this->age(microtime(true)); - if ($row) { - $row->delete(); $deleted++; } } @@ -2278,6 +2303,26 @@ public function orIsNotNull($field) return $this->or($field, 'is not', 'null'); } + public function startsWith($field, $value) + { + return $this->where($field, 'Like', $value . '%'); + } + + public function orStartsWith($field, $value) + { + return $this->or($field, 'Like', $value . '%'); + } + + public function endsWith($field, $value) + { + return $this->where($field, 'Like', '%' . $value); + } + + public function orEndsWith($field, $value) + { + return $this->or($field, 'Like', '%' . $value); + } + public function post($create = false) { $row = $this->create($_POST); @@ -3184,6 +3229,28 @@ public function __invoke(array $data = []) public function fire($event, $concern = null, $return = false) { + $entity = actual("entity.{$this->db}.{$this->table}"); + + $continue = true; + + if (is_object($entity)) { + $methods = get_class_methods($entity); + $method = 'event' . Strings::camelize($event); + + if (in_array($method, $methods)) { + $continue = false; + $result = $entity->$method($concern, $this); + + if ($return) { + return $result; + } + } + } + + if (!$continue) { + return $concern; + } + $key = 'octalia.' . lcfirst(Strings::camelize($this->db . '_' . $this->table)) . '.' . $event; diff --git a/lib/octaliaiterator.php b/lib/octaliaiterator.php index f17a9f1..9dc3cd7 100644 --- a/lib/octaliaiterator.php +++ b/lib/octaliaiterator.php @@ -351,6 +351,10 @@ public function toArray() foreach ($this->getIterator() as $id) { $row = $this->db->read($this->db->row($id)); + if (!$row) { + continue; + } + foreach ($row as $key => $value) { if (fnmatch('*_id', $key)) { $row[str_replace('_id', '', $key)] = em(Inflector::camelize($this->database . '_' . str_replace('_id', '', $key)))->find((int) $value, false); diff --git a/lib/testcase.php b/lib/testcase.php index 522a0a8..e79b73e 100644 --- a/lib/testcase.php +++ b/lib/testcase.php @@ -9,14 +9,14 @@ abstract class TestCase extends PTC abstract public function makeApplication(); - public function em($entity, $new = false) + public function em($entity, $new = true) { return dbMemory($entity, $new); } public function factory($class, $count = 1, $lng = 'fr_FR') { - $model = maker($class); + $model = maker($class, [], false); $faker = faker($lng); $entity = $this->em(