Instead of removing records from your database, the SoftErase Trait will store records in a trashed state, and deleted records can be restored.
SoftErase requires at least Fat-Free v3.* and PHP 5.4+.
It is also required to have a deleted_at
field for all SQL databases
To install, copy the /lib/db/softerase.php
file to the /lib/db/
.
OR use Composer and run composer require acemureithi/f3-softerase
- Define your class
class Book extends \DB\SQL\Mapper{ //Also Works for JIG not tested on Mongo
use \DB\SoftErase; // The SoftErase Trait
}
- Use it!
$db = new \DB\SQL('mysql:host=localhost;port=3306;dbname={db}', 'username', 'password');
$table = 'books';
$mapper = new Book($mysql, $table);
$mapper->book = "The day of the jackal";
$mapper->author = "Cant Remember";
$mapper->save();
$mapper->erase(); //"Record was soft erased"
$mapper->restore(); //Record is back!
$mapper->forceErase(); //Okay Goodbye We cannot restore you anymore
And thats it!
String The Field to store the time of erasing.(Timestamp)
bool Whether to bypass softerase. Set true and records are deleted permanently
Get the (not Deleted) Filter.
@return array|null
Force a hard (normal) erase.
@return bool
$mapper->load(array('title = ?','50 Shades'));
$mapper->forceErase(); //hard Erase
Perform a soft erase.
@return bool
$mapper->load(array('title = ?','50 Shades'));
$mapper->erase(); //Record not permanently deleted but does not show up in find() and load()
Restore a soft-erased record.
@return bool|null
$mapper->load(array('title = ?','50 Shades'));
$mapper->erase(); //Record not permanently deleted but does not show up in find() and load()
$mapper->restore(); //Restores the record now record is in find() and load()
Determine if the instance has been soft-deleted.
@return bool
$mapper->load(array('title = ?','50 Shades'));
$mapper->erase(); //Record not permanently deleted but does not show up in find() and load()
$mapper->erased(); //True
Cursor instance that includes only soft erases.
@return array
Books::onlyErased($mapper);
Also note that load(), erase(), save() and find() are defined in the trait
This trait should be usabe with any class that extends \DB\Cursor
and that includes \DB\SQL\Mapper
, \DB\Mongo\Mapper
, \DB\Jig\Mapper
and also \DB\Cortex
No. Traits are ony supported from php 5.4 onward.
Yes, especially SQL
Databases, add a field deleted_at
(or how you define it in the $deleteTime variable) to avoid field missing errors