TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Best Practice: Trigger-like code. In Model or Controller?
  • I have some code that I want to add to update some model fields with a process that runs on the other model fields. Specifically, I want to geo-encode an address whenever the record is changed and update the lat/lon fields in the model so they get persisted when the model is saved.

    Where is the best place to put my code? In the controller makes sense, but I have multiple controllers talking to the same model (admin, front end, user account pages, etc.) so I don't want to sprinkle it around if I can help it.

    Is there a way to override a property method in ORM so that any change to the address reruns the geocoder?
  • Well, I figured it out. I overrode save() in my Model and check $this->changed for the fields that I am expecting to be changed, then called parent::save() when done.

    I should have more confidence in my self.
  • I found this page, I also like to put trigger like code in Model. I still don't know how to do. Could you show me exactly source code? or some another tips I appreciate it Thanks

  • Hi Katsuhiko, as cchubb discussed above you need to override your models save method. For example, if you have a last_updated property, you can automatically set this to the now() whenever you save the data. Below is a snippet of the code:

    class Model_Users extends ORM {
        public function save(Validation $validation = NULL) {
            $this->last_updated = now();
    
            parent::save($validation);
        }
    }
    

    Hope that helps you out

  • thanks I could make those function,

      public function save()
      {
        parent::save();
        if($this->saved())
        {
                $qucho = ORM::factory('qucho')->where('id', '=', $this->cho_id)->find();
                ++$qucho->sum;
                $qucho->update();
    
          $question = ORM::factory('question')->where('id', '=', $qucho->p_qu_id)->find();
    
                $sm_endans = ORM::factory('smEndans')->where('enqu_id', '=', $question->enqu_id)->where('u_id', '=', $this->u_id)->find();
                $sm_endans->enqu_id = $question->enqu_id;
          $sm_endans->u_id = $this->u_id;
          $sm_endans->q_pt += 0;
          ++$sm_endans->score;
          $sm_endans->name  = Cookie::get('name') ?: 'guest';
          $sm_endans->img  = Cookie::get('img') ?: 'no image';
          $sm_endans->spend_time  += time() - Cookie::get('spend_time');
          $sm_endans->save();
        }
      }
      public function delete()
      {
            $qucho = ORM::factory('qucho')->where('id', '=', $this->cho_id)->find();
        if ($qucho->loaded())
        {
          --$qucho->sum;
              $qucho->update();
        }
        parent::delete();
      }
    
    
  • You could likely save yourself some trouble by setting $saved = parent::save($validation); and returning $saved. Best to have some sort of return value.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion