We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7923
    • 4,213 Posts
    Hello,

    I have my custom tables setup and running with xpdo objects. Now I'd like to override the save method in my own classes to set logged in user id to the record on save and update.

    How should I do it? How can I get access to $modx in my xpdo objects?

    Thanks!


      "He can have a lollipop any time he wants to. That's what it means to be a programmer."
      • 7923
      • 4,213 Posts
      I made this so that I created my own base class that extends xPDOObject and all my objects in the schema extends my base class.

      In this base class I overrided the save() function like so:

        public function save($modx, $cacheFlag= null) {
          $id = $this->getPrimaryKey();
          if (empty($id)) {
            $this->set('created_on', strftime("%Y-%m-%d %H:%M:%S"));
            if (!empty($modx)) {
              $this->set('created_user_id', $modx->user->get('id'));
            }
            $this->set('created_user_ip', $this->getIp());
          }
          $this->set('updated_on', strftime("%Y-%m-%d %H:%M:%S"));
          if (!empty($modx)) {
            $this->set('updated_user_id', $modx->user->get('id'));
          }
          $this->set('updated_user_ip', $this->getIp());
          xPDOObject::save($cacheFlag);
        }
      


      An then every time I save my objects, I pass $modx to the save function.

      Is this a correct way to do it?


        "He can have a lollipop any time he wants to. That's what it means to be a programmer."