Skip to content

Commit

Permalink
Merge pull request #2359 from MGatner/model-trigger-nesting
Browse files Browse the repository at this point in the history
Bugfix Model after event data
  • Loading branch information
MGatner authored Oct 24, 2019
2 parents 49a73ba + 406bdbe commit a424b3e
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,12 @@ public function find($id = null)
$row = $row->getResult($this->tempReturnType);
}

$row = $this->trigger('afterFind', ['id' => $id, 'data' => $row]);
$eventData = $this->trigger('afterFind', ['id' => $id, 'data' => $row]);

$this->tempReturnType = $this->returnType;
$this->tempUseSoftDeletes = $this->useSoftDeletes;

return $row['data'];
return $eventData['data'];
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -438,12 +438,12 @@ public function findAll(int $limit = 0, int $offset = 0)

$row = $row->getResult($this->tempReturnType);

$row = $this->trigger('afterFind', ['data' => $row, 'limit' => $limit, 'offset' => $offset]);
$eventData = $this->trigger('afterFind', ['data' => $row, 'limit' => $limit, 'offset' => $offset]);

$this->tempReturnType = $this->returnType;
$this->tempUseSoftDeletes = $this->useSoftDeletes;

return $row['data'];
return $eventData['data'];
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -475,11 +475,11 @@ public function first()

$row = $row->getFirstRow($this->tempReturnType);

$row = $this->trigger('afterFind', ['data' => $row]);
$eventData = $this->trigger('afterFind', ['data' => $row]);

$this->tempReturnType = $this->returnType;

return $row['data'];
return $eventData['data'];
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -705,11 +705,11 @@ public function insert($data = null, bool $returnID = true)
$data[$this->updatedField] = $date;
}

$data = $this->trigger('beforeInsert', ['data' => $data]);
$eventData = $this->trigger('beforeInsert', ['data' => $data]);

// Must use the set() method to ensure objects get converted to arrays
$result = $this->builder()
->set($data['data'], '', $escape)
->set($eventData['data'], '', $escape)
->insert();

// If insertion succeeded then save the insert ID
Expand All @@ -719,7 +719,7 @@ public function insert($data = null, bool $returnID = true)
}

// Trigger afterInsert events with the inserted data and new ID
$this->trigger('afterInsert', ['id' => $this->insertID, 'data' => $data, 'result' => $result]);
$this->trigger('afterInsert', ['id' => $this->insertID, 'data' => $eventData['data'], 'result' => $result]);

// If insertion failed, get out of here
if (! $result)
Expand Down Expand Up @@ -827,7 +827,7 @@ public function update($id = null, $data = null): bool
$data[$this->updatedField] = $this->setDate();
}

$data = $this->trigger('beforeUpdate', ['id' => $id, 'data' => $data]);
$eventData = $this->trigger('beforeUpdate', ['id' => $id, 'data' => $data]);

$builder = $this->builder();

Expand All @@ -838,10 +838,10 @@ public function update($id = null, $data = null): bool

// Must use the set() method to ensure objects get converted to arrays
$result = $builder
->set($data['data'], '', $escape)
->set($eventData['data'], '', $escape)
->update();

$this->trigger('afterUpdate', ['id' => $id, 'data' => $data, 'result' => $result]);
$this->trigger('afterUpdate', ['id' => $id, 'data' => $eventData['data'], 'result' => $result]);

return $result;
}
Expand Down Expand Up @@ -1564,22 +1564,22 @@ public function countAllResults(bool $reset = true, bool $test = false)
* It is the responsibility of the callback methods to return
* the data itself.
*
* Each $data array MUST have a 'data' key with the relevant
* Each $eventData array MUST have a 'data' key with the relevant
* data for callback methods (like an array of key/value pairs to insert
* or update, an array of results, etc)
*
* @param string $event
* @param array $data
* @param array $eventData
*
* @return mixed
* @throws \CodeIgniter\Database\Exceptions\DataException
*/
protected function trigger(string $event, array $data)
protected function trigger(string $event, array $eventData)
{
// Ensure it's a valid event
if (! isset($this->{$event}) || empty($this->{$event}))
{
return $data;
return $eventData;
}

foreach ($this->{$event} as $callback)
Expand All @@ -1589,10 +1589,10 @@ protected function trigger(string $event, array $data)
throw DataException::forInvalidMethodTriggered($callback);
}

$data = $this->{$callback}($data);
$eventData = $this->{$callback}($eventData);
}

return $data;
return $eventData;
}

//--------------------------------------------------------------------
Expand Down

0 comments on commit a424b3e

Please sign in to comment.