public function doesAlreadyExist(array $criteria) {
return $this->modx->getCount($this->classKey,$criteria);
}public function beforeSave() {
$name = $this->getProperty('name');
$id = $this->getProperty('id');
if ($this->doesAlreadyExist(array('name' => $name, 'id:!=' => $id))) {
return $this->modx->lexicon('bookit.error_name_exists');
}
return parent::beforeSave();
}But, in DoodleUpdateProcessor and DoodleUpdateFromGridProcessor isn't any check if doodle with entered name already exists.Yes, it was beyond the scope of the tutorial.
I tried to call same method (doesAlreadyExist) in update processors, but I get error.That method does not exist in the modObjectUpdateProcessor abstract class.
That method does not exist in the modObjectUpdateProcessor abstract class.Could the method be added in some of the next release of modx? I think, that it could be useful there.
class ItemUpdateProcessor extends modObjectUpdateProcessor {
public $classKey = 'MyExtraItem';
public $languageTopics = array('myextra:default');
public $objectType = 'myextra.item';
public function beforeSave() {
/* make sure a name was specified */
$name = $this->getProperty('name');
if (empty($name)) {
$this->addFieldError('name', $this->modx->lexicon('myextra.item_err_ns_name'));
}
/* if changing name, but new one already exists */
if ($this->alreadyExists('name', $name)) {
$this->addFieldError('name', $this->modx->lexicon('myextra.item_err_ae'));
}
/* make sure a color was specified */
$color = $this->getProperty('color');
if (empty($color)) {
$this->addFieldError('color', $this->modx->lexicon('myextra.item_err_ns_color'));
}
/* if changing color, but new one already exists */
if ($this->alreadyExists('color', $color)) {
$this->addFieldError('color', $this->modx->lexicon('myextra.item_err_ae_color'));
}
$this->object->set('editedon',strftime('%Y-%m-%d %H:%M:%S'));
$this->object->set('editedby',$this->modx->user->get('id'));
return parent::beforeSave();
}
public function alreadyExists($fieldname, $fieldvalue) {
return $this->modx->getCount($this->classKey,array(
$fieldname => $fieldvalue,
'id:!=' => $this->getProperty('id'),
)) > 0;
}
}
return 'ItemUpdateProcessor';