Relation.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace frontendApi\modules\v1\models;
  3. use common\helpers\Cache;
  4. use common\helpers\LoggerTool;
  5. class Relation extends \common\components\ActiveRecord
  6. {
  7. const LOOP_FINISH = 1;
  8. const LOOP_CONTINUE = 2;
  9. private $_limit = 5000;
  10. /**
  11. * 循环推荐网络的父级
  12. * @param $userId
  13. * @param callable $callbackFunc
  14. * @param int $offset
  15. * @return bool
  16. */
  17. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0): bool
  18. {
  19. $allParents = Cache::getAllRelationParents($userId);
  20. $allData = array_slice($allParents, $offset, $this->_limit);
  21. unset($allParents);
  22. if ($allData) {
  23. foreach ($allData as $data) {
  24. $funcResult = $callbackFunc($data);
  25. if ($funcResult === self::LOOP_FINISH) {
  26. return true;
  27. } elseif ($funcResult === self::LOOP_CONTINUE) {
  28. continue;
  29. }
  30. unset($data, $funcResult);
  31. }
  32. unset($allData);
  33. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  34. }
  35. return true;
  36. }
  37. }