We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!

Answered Cascade On Save

    • 38371
    • 42 Posts
    Hello to everyone,

    I have two tables Primary and Secondary. The Secondary table has a foreign key to the Primary. My problem is that when I save the Primary I want the Secondary to be saved, as well.

    The data are submitted from a grid in the MODx Manager that sends data for both of the tables.

    Could someone help me on how to do that using Revo 2.2 and the modObjectCreateProcessor?


    Thanks a lot.

    This question has been answered by leffyrant. See the first response.

    • discuss.answer
      • 38371
      • 42 Posts
      I've found a solution.

      With the appropriate changes to the schema I've overridden the "beforeSave" method of my custom modObjectCreateProcessor like this:
      public function beforeSave() {
          // Creating the secondary object.
          $primary_id = $this->object->get('id');
          $secondary_id = $this->getProperty('secondary_id');
          $secondary = $this->modx->newObject('Secondary');
          $secondary->set('id', $secondary_id);
          $secondary->set('primary_id', $primary_id);
          
          // Adding the secondary to the primary.
          $this->object->addOne($secondary);
      
          return parent :: beforeSave();
      }