Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
- Moving setting of created_by and modified_by to the observer method…
Browse files Browse the repository at this point in the history
…s since they weren’t being handled consistently.

- Using array_walk in *batch methods for slight performance upgrades.
- Making the created_on and modified_on observer methods verify the set_created and set_modified flag is set at the time it’s running. This allows the functionality to be temporarily turned off. Fixes #612.
  • Loading branch information
lonnieezell committed Nov 7, 2013
1 parent bb11942 commit b237eb5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
71 changes: 29 additions & 42 deletions application/core/MY_Model.php
Expand Up @@ -599,12 +599,6 @@ public function insert($data=null)

$data = $this->trigger('before_insert', $data);

if ($this->set_created === true && $this->log_user === true
&& ! array_key_exists($this->created_by_field, $data)
) {
$data[$this->created_by_field] = $this->auth->user_id();
}

// Insert it
$status = $this->db->insert($this->table_name, $data);

Expand Down Expand Up @@ -641,24 +635,9 @@ public function insert($data=null)
*/
public function insert_batch($data=null)
{
$set = array();

// Add the created field
if ($this->set_created === true) {
$set[$this->created_field] = $this->set_date();
}

if ($this->set_created === true && $this->log_user === true) {
$set[$this->created_by_field] = $this->auth->user_id();
}

if ( ! empty($set)) {
foreach ($data as $key => &$record) {
$record = $this->trigger('before_insert', $record);

$data[$key] = array_merge($set, $data[$key]);
}
}
array_walk($data, function(&$record, $key) {
$record = $this->trigger('before_insert', $record);
});

// Insert it
$status = $this->db->insert_batch($this->table_name, $data);
Expand Down Expand Up @@ -696,12 +675,6 @@ public function update($where=NULL, $data=NULL)

$data = $this->trigger('before_update', $data);

// Add the user id if using a modified_by field
if ($this->set_modified === TRUE && $this->log_user === TRUE && !array_key_exists($this->modified_by_field, $data))
{
$data[$this->modified_by_field] = $this->auth->user_id();
}

if ($result = $this->db->update($this->table_name, $data, $where))
{
$this->trigger('after_update', array($data, $result));
Expand Down Expand Up @@ -746,18 +719,10 @@ public function update_batch($data = NULL, $index = NULL)
return FALSE;
}

// Add the modified field
if ($this->set_modified === TRUE && !array_key_exists($this->modified_field, $data))
{
foreach ($data as $key => $record)
{
$data[$key][$this->modified_field] = $this->set_date();
if ($this->log_user === TRUE && !array_key_exists($this->modified_by_field, $data[$key]))
{
$data[$key][$this->modified_by_field] = $this->auth->user_id();
}
}
}
// Run the triggers on each row
array_walk($data, function(&$record, $key) {
$record = $this->trigger('before_update', $record);
});

$result = $this->db->update_batch($this->table_name, $data, $index);
if (empty($result))
Expand Down Expand Up @@ -1193,11 +1158,22 @@ public function skip_validation($skip=true)
*/
public function created_on($row)
{
// Make sure the set_created flag is not temporarily off.
if ( ! $this->set_created)
{
return $row;
}

if ( ! array_key_exists($this->created_field, $row))
{
$row[$this->created_field] = $this->set_date();
}

if ( ! array_key_exists($this->created_by_field, $row) && $this->log_user)
{
$row[$this->created_by_field] = $this->auth->user_id();
}

return $row;
} // end created_on()

Expand All @@ -1213,11 +1189,22 @@ public function created_on($row)
*/
public function modified_on($row)
{
// Make sure the set_created flag is not temporarily off.
if ( ! $this->set_modified)
{
return $row;
}

if ( ! array_key_exists($this->modified_field, $row))
{
$row[$this->modified_field] = $this->set_date();
}

if ( ! array_key_exists($this->modified_by_field, $row) && $this->log_user)
{
$row[$this->modified_by_field] = $this->auth->user_id();
}

return $row;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/_support/database.php
Expand Up @@ -48,6 +48,9 @@ public function platform() { }
public function _error_message() { }
public function affected_rows() { }

public function created_by() {}
public function modified_by() {}

/**
* CI_DB_Result
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/bonfire/core/MY_Model_test.php
Expand Up @@ -335,8 +335,8 @@ public function test_triggered_insert_batch()
);

$triggered_data = array(
array('created_on' => time(), 'title' => 'My Title'),
array('created_on' => time(), 'title' => 'Another Title')
array('title' => 'My Title', 'created_on' => time()),
array('title' => 'Another Title', 'created_on' => time())
);

$this->model->db->expectOnce('insert_batch', array( 'records_table', $triggered_data ));
Expand Down

0 comments on commit b237eb5

Please sign in to comment.