Relation.php 1.2 KB

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