Skip to content
Sebastian G. Marinescu edited this page Feb 22, 2016 · 2 revisions

An Example for an after-save hook.

This get's called when an item is added, useful if you want to update your position-field:

<?php
/**
 * Set the last position on newly saved object
 */

// Get Object & Company
$object = & $modx->getOption('object',$scriptProperties,null);
$properties = $modx->getOption('scriptProperties',$scriptProperties,array());
$postvalues = $modx->getOption('postvalues',$scriptProperties,array());

if ($properties['object_id'] === 'new') {
    
    // Ask for last position
    $q = $modx->newQuery('Clients');
    $q->select(array(
       "max(position)",    
    )); 
    $lastPosition = $modx->getValue($q->prepare());
    
    // Set and Save
    $object->set('position', ++$lastPosition);
    $object->save();
    
}
Clone this wiki locally