当前位置: 首页>>代码示例>>PHP>>正文


PHP Collection::sortBy方法代码示例

本文整理汇总了PHP中Cake\Collection\Collection::sortBy方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::sortBy方法的具体用法?PHP Collection::sortBy怎么用?PHP Collection::sortBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Cake\Collection\Collection的用法示例。


在下文中一共展示了Collection::sortBy方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: getPhotosSorted

 public function getPhotosSorted()
 {
     if (isset($this->photos) and !empty($this->photos)) {
         $photosCollection = new Collection($this->photos);
         return $photosCollection->sortBy(function ($photo) {
             return $photo->sort_order;
         }, SORT_ASC)->toArray();
     }
     return [];
 }
开发者ID:mswagencia,项目名称:photo-gallery,代码行数:10,代码来源:Gallery.php

示例2: _getLastFiveComments

 public function _getLastFiveComments()
 {
     $alias = Inflector::classify($this->source());
     $comment = TableRegistry::get('Social.Comments');
     $comments = $comment->find()->where(['Comments.object_id' => $this->id, 'Comments.object' => $alias])->limit(__MAX_COMMENTS_LISTED)->order('Comments.created DESC')->contain(['Authors' => ['fields' => ['id', 'first_name', 'last_name', 'avatar']]]);
     // Reorder the Comments by creation order
     // (even though we got them by descending order)
     $collection = new Collection($comments);
     $comments = $collection->sortBy('Comment.created');
     return $comments->toArray();
 }
开发者ID:eripoll,项目名称:webiplan,代码行数:11,代码来源:CommentableTrait.php

示例3: get_for_parent

 public function get_for_parent($parent, $page)
 {
     $comments = $this->Comments->find()->where(['Comments.object_id' => $parent])->limit(__MAX_COMMENTS_LISTED)->page($page)->order('Comments.created DESC')->contain(['Authors' => ['fields' => ['id', 'first_name', 'last_name', 'avatar']]]);
     // Reorder the Comments by creation order
     // (even though we got them by descending order)
     $collection = new Collection($comments);
     $comments = $collection->sortBy('Comment.created');
     $view = new View($this->request, $this->response, null);
     $view->layout = 'ajax';
     // layout to use or false to disable
     $view->set('comments', $comments->toArray());
     $data['html'] = $view->render('Social.Comments/get_for_parent');
     $this->layout = 'ajax';
     $this->set('data', $data);
     $this->render('/Shared/json/data');
 }
开发者ID:eripoll,项目名称:webiplan,代码行数:16,代码来源:CommentsController.php

示例4: testSortString

 /**
  * Tests sort
  *
  * @return void
  */
 public function testSortString()
 {
     $items = [['a' => ['b' => ['c' => 4]]], ['a' => ['b' => ['c' => 10]]], ['a' => ['b' => ['c' => 6]]]];
     $collection = new Collection($items);
     $map = $collection->sortBy('a.b.c');
     $this->assertInstanceOf('\\Cake\\Collection\\Collection', $map);
     $expected = [2 => ['a' => ['b' => ['c' => 10]]], 1 => ['a' => ['b' => ['c' => 6]]], 0 => ['a' => ['b' => ['c' => 4]]]];
     $this->assertEquals($expected, iterator_to_array($map));
 }
开发者ID:ripzappa0924,项目名称:carte0.0.1,代码行数:14,代码来源:CollectionTest.php

示例5: statistic

 public function statistic($tournament_id)
 {
     $games = TableRegistry::get('Games')->find()->where(['tournament_id' => $tournament_id]);
     $statistics['matchs'] = 0;
     $statistics['goals'] = 0;
     $statistics['principals_victory'] = 0;
     $statistics['visitors_victory'] = 0;
     $statistics['draws'] = 0;
     foreach ($games as $key => $game) {
         $statistics['matchs']++;
         $statistics['goals'] += $game['score_a'] + $game['score_b'];
         if ($game['score_a'] > $game['score_b']) {
             $statistics['principals_victory']++;
         } else {
             if ($game['score_a'] < $game['score_b']) {
                 $statistics['visitors_victory']++;
             } else {
                 if (!is_null($game['score_a'])) {
                     $statistics['draws']++;
                 }
             }
         }
     }
     $conn = ConnectionManager::get('default');
     $query = $conn->execute('
         SELECT player, team_name, team_shield, IFNULL(sum(goals_pro),0) player_goals_pro, IFNULL(sum(goals_against),0) player_goals_against FROM (
             (
                 SELECT u.id player_id, u.login player, te.name team_name, te.logo team_shield, SUM( score_a ) goals_pro, SUM( score_b ) goals_against
                 FROM games g
                 INNER JOIN tournaments t ON g.tournament_id = t.id
                 INNER JOIN teams_tournaments_users tt ON tt.tournament_id = t.id AND tt.user_id = g.user_a_id
                 INNER JOIN teams te ON te.id = tt.team_id
                 INNER JOIN users u ON u.id = g.user_a_id
                 WHERE t.id =' . $tournament_id . '
                 GROUP BY g.user_a_id
             ) UNION (
                 SELECT u.id player_id, u.login player, te.name team_name, te.logo team_shield, SUM( score_b ) goals_pro, SUM( score_a ) goals_against
                 FROM games g
                 INNER JOIN tournaments t ON g.tournament_id = t.id
                 INNER JOIN teams_tournaments_users tt ON tt.tournament_id = t.id AND tt.user_id = g.user_b_id
                 INNER JOIN teams te ON te.id = tt.team_id
                 INNER JOIN users u ON u.id = g.user_b_id
                 WHERE t.id =' . $tournament_id . '
                 GROUP BY g.user_b_id
             ) ORDER BY goals_pro DESC
         ) a 
         GROUP BY player_id
         ORDER BY player_goals_pro DESC
     ');
     $playersGoals = $query->fetchAll('assoc');
     $statistics['best_atack'] = [];
     $statistics['best_atack'][0]['player'] = $playersGoals[0]['player'];
     $statistics['best_atack'][0]['team'] = $playersGoals[0]['team_name'];
     $statistics['best_atack'][0]['shield'] = $playersGoals[0]['team_shield'];
     $statistics['best_atack'][0]['goals'] = $playersGoals[0]['player_goals_pro'];
     foreach ($playersGoals as $key => $row) {
         if ($statistics['best_atack'][0]['goals'] == $row['player_goals_pro']) {
             $statistics['best_atack'][$key]['player'] = $row['player'];
             $statistics['best_atack'][$key]['team'] = $row['team_name'];
             $statistics['best_atack'][$key]['shield'] = $row['team_shield'];
             $statistics['best_atack'][$key]['goals'] = $row['player_goals_pro'];
         }
     }
     $playersGoalsCollection = new Collection($playersGoals);
     $playersGoals = $playersGoalsCollection->sortBy('player_goals_against', SORT_ASC, SORT_NUMERIC)->toArray();
     $playersGoals = array_values($playersGoals);
     $statistics['best_defense'] = [];
     $statistics['best_defense'][0]['player'] = $playersGoals[0]['player'];
     $statistics['best_defense'][0]['team'] = $playersGoals[0]['team_name'];
     $statistics['best_defense'][0]['shield'] = $playersGoals[0]['team_shield'];
     $statistics['best_defense'][0]['goals'] = $playersGoals[0]['player_goals_against'];
     foreach ($playersGoals as $key => $row) {
         if ($statistics['best_defense'][0]['goals'] == $row['player_goals_against']) {
             $statistics['best_defense'][$key]['player'] = $row['player'];
             $statistics['best_defense'][$key]['team'] = $row['team_name'];
             $statistics['best_defense'][$key]['shield'] = $row['team_shield'];
             $statistics['best_defense'][$key]['goals'] = $row['player_goals_against'];
         }
     }
     $playersGoalsCollection = new Collection($playersGoals);
     $playersGoals = $playersGoalsCollection->sortBy('player_goals_pro', SORT_ASC, SORT_NUMERIC)->toArray();
     $playersGoals = array_values($playersGoals);
     $statistics['worse_atack'] = [];
     $statistics['worse_atack'][0]['player'] = $playersGoals[0]['player'];
     $statistics['worse_atack'][0]['team'] = $playersGoals[0]['team_name'];
     $statistics['worse_atack'][0]['shield'] = $playersGoals[0]['team_shield'];
     $statistics['worse_atack'][0]['goals'] = $playersGoals[0]['player_goals_pro'];
     foreach ($playersGoals as $key => $row) {
         if ($statistics['worse_atack'][0]['goals'] == $row['player_goals_pro']) {
             $statistics['worse_atack'][$key]['player'] = $row['player'];
             $statistics['worse_atack'][$key]['team'] = $row['team_name'];
             $statistics['worse_atack'][$key]['shield'] = $row['team_shield'];
             $statistics['worse_atack'][$key]['goals'] = $row['player_goals_pro'];
         }
     }
     $playersGoalsCollection = new Collection($playersGoals);
     $playersGoals = $playersGoalsCollection->sortBy('player_goals_against', SORT_DESC, SORT_NUMERIC)->toArray();
     $playersGoals = array_values($playersGoals);
     $statistics['worse_defense'] = [];
     $statistics['worse_defense'][0]['player'] = $playersGoals[0]['player'];
//.........这里部分代码省略.........
开发者ID:albertoneto,项目名称:localhost,代码行数:101,代码来源:TournamentsController.php

示例6: getRanking

 public function getRanking($product_id, $videogame)
 {
     $players = [];
     $titles = TableRegistry::get('Users')->find()->select(['Users.id', 'Users.login', 'Users.photo', 'titles' => TableRegistry::get('Users')->find()->func()->count('Users.id')])->join(['Tournaments' => ['table' => 'tournaments', 'type' => 'INNER', 'conditions' => 'Users.id = Tournaments.champion_user_id']])->where(['Tournaments.product_id' => $product_id, 'Tournaments.videogame' => $videogame])->group(['Users.id', 'Users.login', 'Users.photo']);
     foreach ($titles as $key => $value) {
         if (isset($value['login'])) {
             $players[$value['id']]['login'] = $value['login'];
         }
         if (isset($value['photo'])) {
             $players[$value['id']]['photo'] = $value['photo'];
         }
         $players[$value['id']]['titles'] = $value['titles'];
     }
     $victoriesA = TableRegistry::get('Users')->find()->select(['Users.id', 'Users.login', 'Users.photo', 'victories' => TableRegistry::get('Users')->find()->func()->count('Users.id')])->join(['Games' => ['table' => 'games', 'type' => 'INNER', 'conditions' => 'Users.id = Games.user_a_id AND Games.score_a > Games.score_b'], 'Tournaments' => ['table' => 'tournaments', 'type' => 'INNER', 'conditions' => 'Tournaments.id = Games.tournament_id']])->where(['Tournaments.product_id' => $product_id, 'Tournaments.videogame' => $videogame])->group(['Users.id']);
     foreach ($victoriesA as $key => $value) {
         if (isset($value['login'])) {
             $players[$value['id']]['login'] = $value['login'];
         }
         if (isset($value['photo'])) {
             $players[$value['id']]['photo'] = $value['photo'];
         }
         if (isset($players[$value['id']]['victories'])) {
             $players[$value['id']]['victories'] += $value['victories'];
         } else {
             $players[$value['id']]['victories'] = $value['victories'];
         }
     }
     $victoriesB = TableRegistry::get('Users')->find()->select(['Users.id', 'victories' => TableRegistry::get('Users')->find()->func()->count('Users.id')])->join(['Games' => ['table' => 'games', 'type' => 'INNER', 'conditions' => 'Users.id = Games.user_b_id AND Games.score_b > Games.score_a'], 'Tournaments' => ['table' => 'tournaments', 'type' => 'INNER', 'conditions' => 'Tournaments.id = Games.tournament_id']])->where(['Tournaments.product_id' => $product_id, 'Tournaments.videogame' => $videogame])->group(['Users.id']);
     foreach ($victoriesB as $key => $value) {
         if (isset($value['login'])) {
             $players[$value['id']]['login'] = $value['login'];
         }
         if (isset($value['photo'])) {
             $players[$value['id']]['photo'] = $value['photo'];
         }
         if (isset($players[$value['id']]['victories'])) {
             $players[$value['id']]['victories'] += $value['victories'];
         } else {
             $players[$value['id']]['victories'] = $value['victories'];
         }
     }
     $goalA = TableRegistry::get('Users')->find()->select(['Users.id', 'goals' => TableRegistry::get('Users')->find()->func()->sum('Games.score_a')])->join(['Games' => ['table' => 'games', 'type' => 'INNER', 'conditions' => 'Users.id = Games.user_a_id AND Games.score_a > Games.score_b'], 'Tournaments' => ['table' => 'tournaments', 'type' => 'INNER', 'conditions' => 'Tournaments.id = Games.tournament_id']])->where(['Tournaments.product_id' => $product_id, 'Tournaments.videogame' => $videogame])->group(['Users.id']);
     foreach ($goalA as $key => $value) {
         if (isset($value['login'])) {
             $players[$value['id']]['login'] = $value['login'];
         }
         if (isset($value['photo'])) {
             $players[$value['id']]['photo'] = $value['photo'];
         }
         if (isset($players[$value['id']]['goals'])) {
             $players[$value['id']]['goals'] += $value['goals'];
         } else {
             $players[$value['id']]['goals'] = $value['goals'];
         }
     }
     $goalB = TableRegistry::get('Users')->find()->select(['Users.id', 'goals' => TableRegistry::get('Users')->find()->func()->sum('Games.score_b')])->join(['Games' => ['table' => 'games', 'type' => 'INNER', 'conditions' => 'Users.id = Games.user_b_id AND Games.score_b > Games.score_a'], 'Tournaments' => ['table' => 'tournaments', 'type' => 'INNER', 'conditions' => 'Tournaments.id = Games.tournament_id']])->where(['Tournaments.product_id' => $product_id, 'Tournaments.videogame' => $videogame])->group(['Users.id']);
     foreach ($goalB as $key => $value) {
         if (isset($value['login'])) {
             $players[$value['id']]['login'] = $value['login'];
         }
         if (isset($value['photo'])) {
             $players[$value['id']]['photo'] = $value['photo'];
         }
         if (isset($players[$value['id']]['goals'])) {
             $players[$value['id']]['goals'] += $value['goals'];
         } else {
             $players[$value['id']]['goals'] = $value['goals'];
         }
     }
     $playersCollection = new Collection($players);
     $players = $playersCollection->sortBy('titles', SORT_DESC, SORT_NUMERIC)->toArray();
     $players = array_values($players);
     return $players;
 }
开发者ID:albertoneto,项目名称:localhost,代码行数:74,代码来源:SiteController.php

示例7: testSortString

 /**
  * Tests sort
  *
  * @return void
  */
 public function testSortString()
 {
     $items = [['a' => ['b' => ['c' => 4]]], ['a' => ['b' => ['c' => 10]]], ['a' => ['b' => ['c' => 6]]]];
     $collection = new Collection($items);
     $map = $collection->sortBy('a.b.c');
     $this->assertInstanceOf('Cake\\Collection\\Collection', $map);
     $expected = [['a' => ['b' => ['c' => 10]]], ['a' => ['b' => ['c' => 6]]], ['a' => ['b' => ['c' => 4]]]];
     $this->assertEquals($expected, $map->toList());
 }
开发者ID:mhd94,项目名称:cakephp,代码行数:14,代码来源:CollectionTest.php


注:本文中的Cake\Collection\Collection::sortBy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。