We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45613
    • 45 Posts
    I have some fields in some custom tables (createdon, createdby, editedon, editedby) that I want to update whenever the record is saved.

    I was thinking that rather than adding a beforesave call or event for each processor, would it be better to add the code to my model class that extends xPDOSimpleObject to do something like this?
    or is there a better way to put this code in one place and not duplicate in several locations?
    class myTable extends xPDOSimpleObject {
    
    	function save($cacheFlag= null) {
    	    if ($this->isNew()) {
    			$user = $this->modx->user->get('id');
    			$this->set('createdby',$user);
    			$this->set('createdon',strftime('%Y-%m-%d %H:%M:%S'));
    		}
    		$user = $this->modx->user->get('id');
    		$this->set('editedby',$user);
    		$this->set('editedon',strftime('%Y-%m-%d %H:%M:%S'));
    		return parent::save($cacheFlag);
    	}
    }
    [ed. note: bs28723 last edited this post 10 years, 2 months ago.]