codeigniter-base-model
codeigniter-base-model copied to clipboard
Ignore triggers
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?
that would be helpful for me as well. can you share the code if you are not able to make a commit.
thanks!
+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.
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.
Somebody interested may make a back-port.
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.
I like it. Submit a PR, @ivantcholakov, and I'll merge it in
@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.
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!
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