Skip to content

Add the replace() method #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/MY_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,28 @@ public function update_all($data)
return $result;
}

/**
* Replaces a record
* If the record specified by $where can be found, use update,
* Else use Insert to add the record
*
* @param array $where The criteria to find the record we want to replace
* @param array $data The data to update/insert
* @return bool
*/
public function replace($where, $data)
{
$row = $this->get_by($where);
if ($row)
{
return $this->update($row->$this->primary_key, $data);
}
else
{
return ($this->insert($data) > 0);
}
}

/**
* Delete a row from the table by the primary value
*/
Expand Down