1 | <?php |
||
5 | class TrackLastMatch extends AbstractMigration |
||
6 | { |
||
7 | 1 | public function up() |
|
8 | { |
||
9 | 1 | $playersTable = $this->table('players'); |
|
10 | $playersTable |
||
11 | 1 | ->addColumn('last_match', 'integer', array( |
|
12 | 1 | 'signed' => false, |
|
13 | 'limit' => 10, |
||
14 | 'null' => true, |
||
15 | 'comment' => 'The timestamp of the last match this player participated in' |
||
16 | )) |
||
17 | 1 | ->addForeignKey('last_match', 'matches', 'id', array('delete' => 'SET_NULL')) |
|
18 | 1 | ->update(); |
|
19 | |||
20 | 1 | $players = $this->fetchAll('SELECT id FROM players'); |
|
21 | |||
22 | 1 | foreach ($players as $player) { |
|
23 | $this->query( |
||
24 | "UPDATE players SET last_match = ( |
||
25 | SELECT id FROM matches WHERE FIND_IN_SET(${player['id']}, team_a_players) OR FIND_IN_SET(${player['id']}, team_b_players) ORDER BY timestamp DESC LIMIT 1 |
||
26 | ) WHERE id = " . $player['id'] |
||
27 | ); |
||
28 | } |
||
29 | 1 | } |
|
30 | |||
31 | public function down() |
||
39 | } |
||
40 |