Giter Club home page Giter Club logo

orm's Introduction

FuelPHP ORM Including Soft Delete

If you work in finance, healthcare or other "critical" fields, sometimes you're expected to retain data even though the user "deletes" it. This fork supplies that ability by allowing the ORM to "softly" delete a row from the database, that is, mark a column in that row that specify's that row is now "deleted", without actually removing it. That's what this fork is all about. Check out the code below to see how to use the soft delete model and feature. Submit pull requests or issues if you find bugs!

Quickstart

  • Clone this repo into your fuel\packages\ directory. (You'll want to delete the original orm folder in there)
  • Ensure the orm package is being loaded in your APPPATH/config/config.php file.
  • Extend your models with \Orm\Softdelete\Model instead of \Orm\Model
  • If you want to use unix timestamps (1331063441) (Default)
    • Add a field called deleted_at int(11) DEFAULT NULL to any table in the database that you're using Softdelete\Model on.
  • If you wish to use mysql timestamps (YYYY-MM-DD HH:MM:SS)
    • Add a field called deleted_at TIMESTAMP DEFAULT None to any table in the database that you're using Softdelete\Model on.

Features

A few features of the soft delete model:

  • You can still use everything in the normal ORM as usual. No interruptions there, drop this on any code already using the ORM.
  • Completely respects cascade_save and cascade_delete on all relations if the relatied models are instances of \Orm\Softdelete\Model.
<?php

$model->save(); // works exactly the same
$model->delete(); // uses deleted_at field to maintain row persistence in the database
$model->restore(); // sets deleted_at to NULL to show that the row is not deleted
$model->override_delete()->delete(); // calls the original ORM delete, this will nuke the row from the database

Quick Use Example

<?php
class Model_Patient extends \Orm\Softdelete\Model{}

$patient = Model_Patient::forge(
  array(
    'first_name' => 'John',
    'last_name' => 'Doe',
    'pin' => 1234567890,
    'family_doctor' => 'Dr. Seuss'
    'insurance_number' => 0987654321,
  )
);

// save the patient to the database
$patient->save();

// The delete method has been over-ridden by the soft delete for this model
// This effectively sets $_soft_delete_property to either a mysql or unix timestamp in the row
$patient->delete();

// Restore the patient from being soft-deleted
$patient->restore();

Example Model

<?php
/**
 * Let's use a healthcare example
 */
class Model_Patient extends \Orm\Softdelete\Model
{

    /**
     * Set this field as the column you want to use to mark the row as deleted
     * Default : 'deleted_at'
     */
    protected static $_soft_delete_column = 'deleted_at'; // default
    
    /**
     * Choose whether to use a mysql timestamp (YYYY-MM-DD HH:MM:SS) or a unix timestamp (1331063441) 
     * @see http://en.wikipedia.org/wiki/Unix_time
     */
    protected static $_mysql_timestamp = false; // default
    
    // other
    protected static $_observers = array('Orm\\Observer_CreatedAt', 'Orm\\Observer_UpdatedAt');
        
    protected static $_properties = array(
      'first_name',
      'last_name',
      'pin',
      'family_doctor',
      'insurance_number',
    );
  
}

orm's People

Contributors

abdel avatar bencorlett avatar cloudmanic avatar crynobone avatar davidia avatar dhrrgn avatar dudeami avatar el2ro avatar enygma avatar fesplugas avatar five07 avatar frankdejonge avatar huglester avatar jamierumbelow avatar jeffutter avatar jesseobrien avatar johanandre avatar kapv89 avatar leo-nard avatar logicbox avatar lukeholder avatar mikebranderhorst avatar phil-lavin avatar santiagofs avatar snrp avatar tarnfeld avatar theycallmeswift avatar tomschlick avatar ushis avatar wanwizard avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

fuel-packages

orm's Issues

Restore Relations

Need to make the restore() function work up through cascading relations.

I think this will have to be done via each relation having it's own restore() function to be called from the model.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.