Skip to content

Commit

Permalink
corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Jun 8, 2017
1 parent 599b82f commit 9193788
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
79 changes: 73 additions & 6 deletions lib/octalia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -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++;
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions lib/octaliaiterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 9193788

Please sign in to comment.