Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore triggers #113

Open
donaldallen opened this issue Mar 20, 2013 · 9 comments
Open

Ignore triggers #113

donaldallen opened this issue Mar 20, 2013 · 9 comments

Comments

@donaldallen
Copy link

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?

@keyurshah
Copy link

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

thanks!

@ivantcholakov
Copy link

+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
Copy link

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 added a commit to ivantcholakov/codeigniter-base-model that referenced this issue Jul 19, 2014
@ivantcholakov
Copy link

Somebody interested may make a back-port.

@ivantcholakov
Copy link

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.

@jamierumbelow
Copy link
Owner

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

@ivantcholakov
Copy link

@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.

@jamierumbelow
Copy link
Owner

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 git@github.com: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!

@ivantcholakov
Copy link

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 added a commit to ivantcholakov/codeigniter-base-model that referenced this issue Jul 30, 2014
ivantcholakov added a commit to ivantcholakov/codeigniter-base-model that referenced this issue Jul 30, 2014
ivantcholakov added a commit to ivantcholakov/codeigniter-base-model that referenced this issue Jul 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants