We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29661
    • 116 Posts
    I am trying new doodles tutorial (http://rtfm.modx.com/display/revolution20/Developing+an+Extra+in+MODX+Revolution%2C+Part+II) for Revo 2.2 and I think that is a little mistake in documentation of this tutorial.

    DoodleCreateProcessor is checking if exists doodle with same name (by doesAlreadyExist method) and if yes, return error.

    But, in DoodleUpdateProcessor and DoodleUpdateFromGridProcessor isn't any check if doodle with entered name already exists.

    I tried to call same method (doesAlreadyExist) in update processors, but I get error.

    When I create doesAlreadyExist method (that is below) in modObjectUpdateProcessor class (/core/model/modx/modprocessor.class.php) error disappeared and everything works fine.

    public function doesAlreadyExist(array $criteria) {
    	return $this->modx->getCount($this->classKey,$criteria);
    }


    And how to use:
    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();
    }
    [ed. note: TheBoxer last edited this post 14 years, 8 months ago.]
      --
      John
      @theboxer
      • 28215
      • 4,149 Posts
      Quote from: TheBoxer at Jan 09, 2012, 04:35 PM
      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.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 29661
        • 116 Posts
        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.
          --
          John
          @theboxer
          • 10378
          • 375 Posts
          I know It's a late answer, but as I also struggled around with this problem - here is a fully working modObjectUpdateProcessor class:

          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';
          
            Freelancer @bitego http://www.bitego.com
            ---
            GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
            More infos here: http://www.bitego.com/extras/goodnews/