как пересохранить 2000 ресурсов ?

Доброго дня, суть вопроса в том как я могу программно пересохранить 2000 ресурсов, понадобилось добавить еще одно поле в которое должно подтягиваться значение ответа от сервера яндекса, но плагин записывает значение при сохранении ресурса, заранее спасибо
Сергей Скат
22 января 2015, 10:12
modx.pro
6
3 263
0

Комментарии: 16

Fi1osof
22 января 2015, 15:42
5
+3
Ставите Console.
Выполняете код:
ini_set('max_execution_time', 0);
ignore_user_abort(true);
$c = 'modResource';
$q = $modx->newQuery($c);
$q->sortby('id');
$q->select(array(
	"{$c}.*",
));
$s = $q->prepare();
$s->execute();
while($row = $s->fetch(2))
	$response = $modx->runProcessor('resource/update', $row);
	if($response->isError()){
		print_r($response->getResponse());
		return;
	}
	$modx->error->reset();
}
Так как у вас документов много, советую сначала прогнать только корневые документы (добавить в запрос $q->where(array('parent' => 0));), а потом по каждому разделу в отдельности, указывая соответствующий parent.
    Сергей Скат
    22 января 2015, 16:16
    0
    а нет разделов, спасибо большое !)
      Fi1osof
      22 января 2015, 16:17
      0
      Ну ОК.
      Пожалуйста.
        Fi1osof
        22 января 2015, 16:18
        0
        И не забываем про бэкапы:)
      Игорь
      30 ноября 2017, 09:19
      0
      А если вложенность 5 уровней и больше, и по каждому «пробежаться» — очень долго получается. Как указать один корневой раздел и условие по id шаблона (независимо от уровня вложенности). Не подскажете?
      ====
      То есть раздел, у которого id=2 и внутри него пересохранить все ресурсы, у которых template=4
      ====
      Заранее большое спасибо.
        g0dzilla
        30 ноября 2017, 10:04
        +2
        docs.modx.com/xpdo/2.x/class-reference/xpdoquery/xpdoquery.where

        Вообще заметил тендецию, в последнее время, что старые посты некрофилируют. Все давно уже в доках
          Игорь
          30 ноября 2017, 10:11
          0
          Спасибо. А на конкретном примере как будет выглядеть код? Спасибо
            Игорь
            30 ноября 2017, 10:28
            0
            Если я правильно понял. Но что-то не работает.
            <?php
            ini_set('max_execution_time', 0);
            ignore_user_abort(true);
            $c = 'modResource';
            $q = $modx->newQuery($c);
            $q->where(array('parent' => 2));
            $q->where(array('template' => 4));
            $q->sortby('id');
            $q->select(array(
            	"{$c}.*",
            ));
            $s = $q->prepare();
            $s->execute();
            while($row = $s->fetch(0))
            	$response = $modx->runProcessor('resource/update', $row);
            	if($response->isError()){
            		print_r($response->getResponse());
            		return;
            	}
            	$modx->error->reset();
              Алексей Ерохин
              30 ноября 2017, 10:40
              +1
              $resources = $modx->getChildIds(2, 10, array('context'=>'web'));
              ....
              $q->where(array('id:IN' =>$resources));
              $q->where(array('template' => 4));
                Игорь
                30 ноября 2017, 10:43
                0
                Покажусь нубом, но тем не менее… В Console не выполняет
                <?php
                ini_set('max_execution_time', 0);
                ignore_user_abort(true);
                $c = 'modResource';
                $q = $modx->newQuery($c);
                $resources = $modx->getChildIds(2, 10, array('context'=>'web'));
                $q->where(array('id:IN' =>$resources));
                $q->where(array('template' => 4));
                $q->sortby('id');
                $q->select(array(
                	"{$c}.*",
                ));
                $s = $q->prepare();
                $s->execute();
                while($row = $s->fetch(0))
                	$response = $modx->runProcessor('resource/update', $row);
                	if($response->isError()){
                		print_r($response->getResponse());
                		return;
                	}
                	$modx->error->reset();
                Огромное спасибо за отклик. Но как будет полный вариант выглядеть?
                  Алексей Ерохин
                  30 ноября 2017, 10:54
                  +1
                  while($row = $s->fetch(2)) {
                  	$response = $modx->runProcessor('resource/update', $row);
                  	if($response->isError()){
                  		print_r($response->getResponse());
                  		return;
                  	}
                  	$modx->error->reset();
                  }
                  Скобочки фигурные потеряли, и в fetch нужно 2 (PDO::FETCH_ASSOC) указывать, чтобы ассоциативный массив получить
                    Игорь
                    30 ноября 2017, 10:58
                    0
                    При
                    <?php
                    ini_set('max_execution_time', 0);
                    ignore_user_abort(true);
                    $c = 'modResource';
                    $q = $modx->newQuery($c);
                    $resources = $modx->getChildIds(2, 10, array('context'=>'web'));
                    $q->where(array('id:IN' =>$resources));
                    $q->where(array('template' => 4));
                    $q->sortby('id');
                    $q->select(array(
                    	"{$c}.*",
                    ));
                    $s = $q->prepare();
                    $s->execute();
                    while($row = $s->fetch(0))
                    	$response = $modx->runProcessor('resource/update', $row);
                    	if($response->isError()){
                    		print_r($response->getResponse());
                    		return;
                    	}
                    	$modx->error->reset();
                    }
                    Ругается на синтаксис: Parse error: syntax error, unexpected '}' in .../core/components/console/processors/exec.class.php(24): eval()'d code
                      Алексей Ерохин
                      30 ноября 2017, 11:00
                      +1
                      Скобочка фигурная }
                      while($row = $s->fetch(0)) {
                      Посмотрите внимательнее, пожалуйста.
                        Игорь
                        30 ноября 2017, 11:08
                        0
                        Алексей, благодарю за помощь.
                        Висит и не завершает в Console
                        <?php
                        ini_set('max_execution_time', 0);
                        ignore_user_abort(true);
                        $c = 'modResource';
                        $q = $modx->newQuery($c);
                        $resources = $modx->getChildIds(2, 10, array('context'=>'web'));
                        $q->where(array('id:IN' =>$resources));
                        $q->where(array('template' => 4));
                        $q->sortby('id');
                        $q->select(array(
                        	"{$c}.*",
                        ));
                        $s = $q->prepare();
                        $s->execute();
                        while($row = $s->fetch(0)) {
                        	$response = $modx->runProcessor('resource/update', $row);
                        	if($response->isError()){
                        		print_r($response->getResponse());
                        		return;
                        	}
                        	$modx->error->reset();
                        }
                          Игорь
                          30 ноября 2017, 15:03
                          0
                          Так и не пойму, в чем проблема. Поправил то, о чем Вы написали, но не работает.
                          <?php
                          ini_set('max_execution_time', 0);
                          ignore_user_abort(true);
                          $c = 'modResource';
                          $q = $modx->newQuery($c);
                          $resources = $modx->getChildIds(2, 10, array('context'=>'web'));
                          $q->where(array('id:IN' =>$resources));
                          $q->where(array('template' => 4));
                          $q->sortby('id');
                          $q->select(array(
                          	"{$c}.*",
                          ));
                          $s = $q->prepare();
                          $s->execute();
                          while($row = $s->fetch(2)) {
                          	$response = $modx->runProcessor('resource/update', $row);
                          	if($response->isError()){
                          		print_r($response->getResponse());
                          		return;
                          	}
                          	$modx->error->reset();
                          }
          Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
          16