Welcome Guest, Not a member yet? Register   Sign In
"soft" delete
#1

[eluser]Unknown[/eluser]
Hi, I have a question that I'm sure has a simple answer:

When I delete an element from the database via
Code:
$this->delete()
, I'd like to set a boolean deleted field to 1, instead of totally erasing it.

I'm using DataMapper ORM, by the way.

Any hints?

thanks!
Antonio
#2

[eluser]osci[/eluser]
simple answer: don't use delete. do update.

but you have to handle it in your selects too (exclude deleted=1)

not simple answer: change the datamapper code to handle this.
#3

[eluser]gRoberts[/eluser]
I wrote my own base_model which exposese simple methods to get/set data.

Get - Returns ALL records
GetActive - Returns all records where Removed is null
Save - Inserts or Updates depending on primary key existance
Remove - Sets the Removed Column to the current date and saves.

Any additional functions use the above methods as a base. This way, i can ensure, that the data I am getting
truncates any rows that are marked as removed.
#4

[eluser]pmoroom[/eluser]
[quote author="osci" date="1309532664"]simple answer: don't use delete. do update.

but you have to handle it in your selects too (exclude deleted=1)

not simple answer: change the datamapper code to handle this.[/quote]

Or he could override $this->delete to have it do what he needs right?
#5

[eluser]WanWizard[/eluser]
I capture this scenario in my models. In the delete() method I check if the table contains a 'deleted' column. If so, I run an UPDATE instead of a DELETE query.

The controller is not aware of this, it just calls the delete() method of the model. The opposite happens at retrieval, where a WHERE clause is inserted with 'AND deleted = 0' to filter out deleted records.
#6

[eluser]mightyrsm[/eluser]
Hi @WanWizard.

You have really given a great ORM thanks!
I was hoping you could share, or update the Soft delete code implementation.

Thanks a lot again
Rahul
#7

[eluser]WanWizard[/eluser]
Share what?

That soft delete hack? I'm not sure we still have it. We switched to another framework over two years ago, and since then all CI code has been replaced. We don't do CI anymore.

But I will have a look...
#8

[eluser]mightyrsm[/eluser]
I was just starting to really like Datamapper ORM for codeigniter! What are the future implications, support wise if I stick with Datamapper ORM and codeigniter..

I saw you are one of the major contributors to FuelPHP.
Would you suggest if working with Laravel or FuelPHP will be better, than Datamapper ORM on CI.

Thanks again
#9

[eluser]WanWizard[/eluser]
The implications are that I will probably (for the forseeable future) try to find time to maintain the current version of Datamapper (as in: fix serious bugs), but that you should not expect any new functionality or new versions from me anymore.

Datamapper is old code (5 years +), and would really need a full rewrite. It is also fully integrated in CI at the moment, and the way the current CI development is going, that is going to be tough or impossible.

I am the current project lead for FuelPHP, and I'm amongst other things responsible for it's ORM, which is vastly superior to Datamapper. The next version will be framework agnostic, so it could be easily adapted to work with CI as well.

This is the CI forum, so I'm not going to do any suggestions regarding other frameworks. We (our company) had our reasons to dump CI as framework of preference. These reasons may not be relevant for you.
#10

[eluser]mightyrsm[/eluser]
Thanks for replying to my queries.

Its made me look at FuelPHP much more seriously.
But most of all, your contributions to ORMs like Datamapper and FuelPHP are really appreciated. Great work!




Theme © iAndrew 2016 - Forum software by © MyBB