We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11055 ☆ A M B ☆
    • 3,112 Posts
    The xpdo's removeCollection only returns number of removed objects.
    Is there any way to retrieved the removed objects as same as getCollection?

    The case is that I'm using based class processor to remove data, and the output is parsed using the response processor which expects an array of processed objects.

    I assume a temporary object would handle the removed objects and be used as the removeCollection's output.

    For your convenient, this is my processor:
    <?php
    
    class WebUsersOnlineRemoveTimeoutProcessor extends modObjectGetListProcessor {
    
        public $classKey = 'OnlineUsers';
        public $languageTopics = array();
        public $defaultSortField = 'user_name';
        public $checkListPermission = false;
        public $objectType = 'modjochat.WebUsersOnlineRemoveTimeout';
    
        /**
         * Get the data of the query
         * @return array
         */
        public function getData() {
            $data = array();
    
            /* query for chunks */
            $c = $this->modx->newQuery($this->classKey);
            $c = $this->prepareQueryBeforeCount($c);
            $data['total'] = $this->modx->getCount($this->classKey, $c);
            $c = $this->prepareQueryAfterCount($c);
    
            $sortClassKey = $this->getSortClassKey();
            $sortKey = $this->modx->getSelectColumns($sortClassKey, $this->getProperty('sortAlias', $sortClassKey), '', array($this->getProperty('sort')));
            if (empty($sortKey))
                $sortKey = $this->getProperty('sort');
            $c->sortby($sortKey, $this->getProperty('dir'));
    
    $c->prepare();
    $sql = $c->toSQL();
    
    $this->modx->log(modX::LOG_LEVEL_ERROR, __FILE__ . ' ');
    $this->modx->log(modX::LOG_LEVEL_ERROR, __METHOD__ . ' ');
    $this->modx->log(modX::LOG_LEVEL_ERROR, __LINE__ . ': $sql ' . $sql);
    
            $removed = $this->modx->removeCollection($this->classKey, $c);
            if ($removed) {
    $this->modx->log(modX::LOG_LEVEL_ERROR, __LINE__ . ': $removed ' . print_r($removed, 1));
            }
            $data['results'] = array();
            return $data;
        }
    
        /**
         * Can be used to adjust the query prior to the COUNT statement
         *
         * @param xPDOQuery $c
         * @return xPDOQuery
         */
        public function prepareQueryBeforeCount(xPDOQuery $c) {
            $timeOut = time();
            $c->where(array(
                'timeout:<' => $timeOut
            ));
            return $c;
        }
    
    }
    
    return 'WebUsersOnlineRemoveTimeoutProcessor';
    [ed. note: goldsky last edited this post 13 years, 8 months ago.]
      Rico
      Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
      MODx is great, but knowing how to use it well makes it perfect!

      www.virtudraft.com

      Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

      Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

      Maintainter/contributor of Babel

      Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
      • 11055 ☆ A M B ☆
      • 3,112 Posts
      Well, I ended up using this way:
      <?php
      
      class WebUsersOnlineRemoveTimeoutProcessor extends modObjectGetListProcessor {
      
          public $classKey = 'OnlineUsers';
          public $languageTopics = array();
          public $defaultSortField = 'user_name';
          public $checkListPermission = false;
          public $objectType = 'modjochat.WebUsersOnlineRemoveTimeout';
      
          /**
           * Get the data of the query
           * @return array
           */
          public function getData() {
              $data = array();
      
              /* query for chunks */
              $c = $this->modx->newQuery($this->classKey);
              $c = $this->prepareQueryBeforeCount($c);
              $data['total'] = $this->modx->getCount($this->classKey, $c);
              $c = $this->prepareQueryAfterCount($c);
      
              $sortClassKey = $this->getSortClassKey();
              $sortKey = $this->modx->getSelectColumns($sortClassKey, $this->getProperty('sortAlias', $sortClassKey), '', array($this->getProperty('sort')));
              if (empty($sortKey))
                  $sortKey = $this->getProperty('sort');
              $c->sortby($sortKey, $this->getProperty('dir'));
      
              $collection = $this->modx->getCollection($this->classKey, $c);
              if ($collection) {
                  foreach ($collection as $object) {
                      if ($object->remove() == false) {
                          $this->modx->log(modX::LOG_LEVEL_ERROR, __FILE__ . ' ');
                          $this->modx->log(modX::LOG_LEVEL_ERROR, __METHOD__ . ' ');
                          $this->modx->log(modX::LOG_LEVEL_ERROR, __LINE__ . ': error removing user_name from OnlineUsers: ' . $object->get('user_name'));
                      }
                  }
              }
              $data['results'] = $collection;
              return $data;
          }
      
          /**
           * Can be used to adjust the query prior to the COUNT statement
           *
           * @param xPDOQuery $c
           * @return xPDOQuery
           */
          public function prepareQueryBeforeCount(xPDOQuery $c) {
              $timeOut = time();
              $c->where(array(
                  'timeout:<' => $timeOut
              ));
              return $c;
          }
      
      }
      
      return 'WebUsersOnlineRemoveTimeoutProcessor';


      But I think there should be more elegant way.
        Rico
        Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
        MODx is great, but knowing how to use it well makes it perfect!

        www.virtudraft.com

        Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

        Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

        Maintainter/contributor of Babel

        Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
        • 11055 ☆ A M B ☆
        • 3,112 Posts
        just to let you know, the where condition doesn't work on removeCollection.
        it keeps removing all the data inside the table.
        i'm using PHP 5.4, btw.
          Rico
          Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
          MODx is great, but knowing how to use it well makes it perfect!

          www.virtudraft.com

          Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

          Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

          Maintainter/contributor of Babel

          Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.