示例#1
0
 /**
  * Remove filter, if him not used
  *
  * @param int $limit
  * @return string
  */
 public static function removeUnusedFilter($limit = 50)
 {
     if (($limit = (int) $limit) < 0) {
         return;
     }
     $connection = \Bitrix\Main\Application::getConnection();
     $sqlHelper = $connection->getSqlHelper();
     $sql = "\n            SELECT t1.ID\n            FROM b_citfact_filter_subscribe as t1\n            WHERE NOT EXISTS (\n                SELECT t2.ID\n                FROM b_citfact_filter_subscribe_user as t2\n                WHERE t2.FILTER_ID = t1.ID\n            )\n            LIMIT 0," . $limit . "\n        ";
     $fitlerResult = $connection->query($sql);
     while ($filter = $fitlerResult->fetch()) {
         Model\SubscribeTable::delete(array('ID' => $filter['ID']));
     }
     return "Citfact\\FilterSubscribe\\Agent::removeUnusedFilter({$limit})";
 }
 /**
  * Remove filter user and relationships
  *
  * @param int $id
  * @return \Bitrix\Main\Entity\DeleteResult
  * @throws \InvalidArgumentException
  */
 public function removeFilterUser($id)
 {
     $filterUser = Model\SubscribeUserTable::getById($id)->fetch();
     if ($this->isEmptyResult($filterUser)) {
         throw new \InvalidArgumentException('Invalid filter user id');
     }
     $queryBuilder = new Entity\Query(Model\SubscribeStackTable::getEntity());
     $subscribeStack = $queryBuilder->setSelect(array('ID'))->setFilter(array('FILTER_USER_ID' => $filterUser['ID']))->exec();
     while ($row = $subscribeStack->fetch()) {
         Model\SubscribeStackTable::delete(array('ID' => $row['ID']));
     }
     $result = Model\SubscribeUserTable::delete(array('ID' => $filterUser['ID']));
     $queryBuilder = new Entity\Query(Model\SubscribeUserTable::getEntity());
     $filterUserResult = $queryBuilder->registerRuntimeField('cnt', array('data_type' => 'integer', 'expression' => array('count(%s)', 'ID')))->setSelect(array('ID', 'cnt'))->setFilter(array('FILTER_ID' => $filterUser['FILTER_ID']))->exec()->fetch();
     if ($filterUserResult['cnt'] <= 0) {
         Model\SubscribeTable::delete(array('ID' => $filterUser['FILTER_ID']));
     }
     return $result;
 }