marko aleksic Mon Oct 24 14:56:42 -0400 2011

Subject: Getter - Get the rows where attribute meets criteria


I want to ask if its possible to implement a getter function, which will filter all the rows retrieved from the DB by specific attribute value.

To be exact, I have an entity(lets say Items) which has attribute STATUS - ENUM[active,delete].

So, whenever I do $rows = Item::all();,
I dont have to write a conditions 'where'="'status='active'", eg. Item::find('all',array('conditions...)
Meaning, I want the Model always to retrieve the 'active' rows.

I image the function would be
function get_status() {
$this->status{'active'}; (This is just to portray my logic)

Can you please help me with your opinions or solutions to this problem.

Thank you very much

Beau Beauchamp Mon Oct 24 19:50:28 -0400 2011


I am doing the same thing. However, I don't see any way around making calls without having to write the conditions (WHERE clause). I'm also implementing a soft delete and EVERY call has to be conditioned with:

"... status = 1 AND delete = 0"

Sometimes you just gotta write the code. Let me know if you come up with something smooth, however. ;)


marko aleksic Tue Oct 25 06:58:20 -0400 2011

Hello Beau,

After long pondering on the issue, I did the following.

Since this model is going to be used very frequently, I had to get rid of the soft delete(STATUS attribute) functionality.
Yet, I wanted to keep the deleted records, so I created an archive table and a before DELETE trigger (MySQL).

This way I keep the deleted rows.

For the other models (eg. Categories of Products) which will support the main functionality of the app, I kept the STATUS attribute. I did this because they does not have complex db queries and are not used that often.

Until I come up with better solution, this will work for me.

Hope it help you in some way.

p.s. I realized that the STATUS attribute complicates things more for complex and big tables that are used often, than it brings benefits.