Skip to content

Commit

Permalink
skip_observers() feature implementation. See jamierumbelow#113
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantcholakov committed Jul 30, 2014
1 parent 3d58618 commit 25dd66a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ Observers can also take parameters in their name, much like CodeIgniter's Form V
return $row;
}

**By using the scope skip_observers() triggering of all the attached/registered observers can be disabled. For example:

```php
// A user logs in. We want to store a timestamp of this moment,
// but we don't the registered built-in observer 'updated_at' to be triggered,
// i.e. we don't want the field 'updated_at' to change its value.
$this->users->skip_observers()->update($id, array('last_login_at' => date('Y-m-d H:i:s')));
```

Validation
----------

Expand Down
24 changes: 23 additions & 1 deletion core/MY_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class MY_Model extends CI_Model

protected $callback_parameters = array();

/**
* Support for skip_observers() scope.
*/
protected $_temporary_skip_observers = FALSE;

/**
* Protected, non-modifiable attributes
*/
Expand Down Expand Up @@ -237,10 +242,14 @@ public function insert($data, $skip_validation = FALSE)
*/
public function insert_many($data, $skip_validation = FALSE)
{
$skip_observers = $this->_temporary_skip_observers;

$ids = array();

foreach ($data as $key => $row)
{
$this->_temporary_skip_observers = $skip_observers;

$ids[] = $this->insert($row, $skip_validation, ($key == count($data) - 1));
}

Expand Down Expand Up @@ -455,6 +464,9 @@ public function with($relationship)
return $this;
}

// This observer is to be suppressed by skip_observers() scope too.
// This might change if there is a good/valid use-case, but let us not
// complicate code for now.
public function relate($row)
{
if (empty($row))
Expand Down Expand Up @@ -680,6 +692,15 @@ public function only_deleted()
return $this;
}

/**
* Disables triggering of all the attached/registered observers.
*/
public function skip_observers()
{
$this->_temporary_skip_observers = TRUE;
return $this;
}

/* --------------------------------------------------------------
* OBSERVERS
* ------------------------------------------------------------ */
Expand Down Expand Up @@ -809,7 +830,7 @@ public function limit($limit, $offset = 0)
*/
public function trigger($event, $data = FALSE, $last = TRUE)
{
if (isset($this->$event) && is_array($this->$event))
if (!$this->_temporary_skip_observers && isset($this->$event) && is_array($this->$event))
{
foreach ($this->$event as $method)
{
Expand Down Expand Up @@ -976,6 +997,7 @@ protected function _reset_state()
$this->_temporary_return_type = $this->return_type;
$this->_temporary_with_deleted = FALSE;
$this->_temporary_only_deleted = FALSE;
$this->_temporary_skip_observers = FALSE;
}

}

0 comments on commit 25dd66a

Please sign in to comment.