codeigniter-base-model icon indicating copy to clipboard operation
codeigniter-base-model copied to clipboard

Ignore triggers

Open donaldallen opened this issue 11 years ago • 9 comments

I have both a $before_create and $before_update. Sometimes I want to update a row without touching those triggers. I couldn't see a way to do it with anything already built in, so I added a $skip_triggers to function update. Should I put in a PR for this?

donaldallen avatar Mar 20 '13 19:03 donaldallen

that would be helpful for me as well. can you share the code if you are not able to make a commit.

thanks!

keyurshah avatar Apr 11 '13 16:04 keyurshah

+1

The first method: A scope 'ignore_observers()' should be created and then it should be applied as a condition within the internal method trigger(). This is the easier method, only one place is to be targeted. But there is one useless passing and returning data - it seems to me not a big deal.

The second method: The same scope 'ignore_observers()' to be applied as a condition on all the $this->trigger(....) calls. but this way future inconsistencies are possible if other features are added.

I prefer the first method.

ivantcholakov avatar Jul 19 '14 16:07 ivantcholakov

A second thought: A combination of the both ways would be the best. skip_observers() as scope name matches better.

Edit: I choose the first method, because it is simpler. Performance optimizations may be done later, if they are needed.

ivantcholakov avatar Jul 19 '14 16:07 ivantcholakov

Somebody interested may make a back-port.

ivantcholakov avatar Jul 19 '14 18:07 ivantcholakov

A real usage: I've got a table with user accounts and the corresponding model has the observers 'created_at', 'created_by', 'updated_at', 'updated_by'. Also I have a field 'last_logged_in_at' that stores current time when user logs in. While this field is to be updated, I don't want the fields 'updated_at' and 'updated_by' to get changed, and skip_observers() helps for that. As an alternative, the basic CodeIgniter's query builder may be used, but code will loose its beauty.

Other similar cases: Storing number of page previews, "likes" or rates, without touching anything else within records.

ivantcholakov avatar Jul 27 '14 20:07 ivantcholakov

I like it. Submit a PR, @ivantcholakov, and I'll merge it in

jamierumbelow avatar Jul 29 '14 19:07 jamierumbelow

@jamierumbelow

My implementation is on a clone that is getting quite different. Before implementing this feature would you allow minor refactoring? It would be easier if there is a method protected function _reset_state() { ... } that sets all the internal scope variables to their default values.

ivantcholakov avatar Jul 29 '14 19:07 ivantcholakov

You can submit a pull request by cherry picking the commit into a separate branch, and then PRing on that branch. Like this:

$ git remote add jamierumbelow [email protected]:jamierumbelow/codeigniter-base-model.git
$ git checkout -b skip_observers jamierumbelow/master
$ git cherry-pick 5a8c1a4d618689731f47599e0ec574b977ffb2a1
$ git push origin skip_observers

Then switch onto skip_observers in your GH repo, and submit a PR from there!

jamierumbelow avatar Jul 29 '14 20:07 jamierumbelow

Just a note for me:

$ git checkout -b skip_observers jamierumbelow/master
fatal: Cannot update paths and switch to branch 'skip_observers' at the same time.
Did you intend to checkout 'jamierumbelow/master' which can not be resolved as commit?

This works:

$ git fetch jamierumbelow master:skip_observers

ivantcholakov avatar Jul 30 '14 01:07 ivantcholakov