-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeleteRelatedRoomsTask.php
More file actions
141 lines (119 loc) · 3.58 KB
/
DeleteRelatedRoomsTask.php
File metadata and controls
141 lines (119 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* 関連テーブルのデータを削除する
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('AppShell', 'Console/Command');
App::uses('RoomsLibDeleteRoomTables', 'Rooms.Lib');
App::uses('RoomsLibLog', 'Rooms.Lib');
App::uses('SiteSettingUtil', 'SiteManager.Utility');
/**
* 関連テーブルのデータを削除する
*
* @property RoomDeleteRelatedTable $RoomDeleteRelatedTable
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Rooms\Console\Command
*/
class DeleteRelatedRoomsTask extends AppShell {
/**
* 使用するモデル
*
* @var array
*/
public $uses = [
'Rooms.RoomDeleteRelatedTable',
];
/**
* DeleteRoomTablesを操作するライブラリ
*
* @var RoomsLibDeleteRoomTables
*/
private $__RoomsLibDeleteRoomTables;
/**
* 一度の処理する件数
*
* @var int
*/
const PROCESS_COUNT = 100;
/**
* タスク実行
*
* @return void
*/
public function execute() {
$this->__RoomsLibDeleteRoomTables =
new RoomsLibDeleteRoomTables($this->RoomDeleteRelatedTable, $this);
$tableName = $this->RoomDeleteRelatedTable->tablePrefix .
$this->RoomDeleteRelatedTable->table;
$aliasName = $this->RoomDeleteRelatedTable->alias;
$result = $this->RoomDeleteRelatedTable->query(
"SELECT COUNT(*) count_num FROM {$tableName} AS {$aliasName}" .
" WHERE {$aliasName}.end_time IS NULL" .
//" AND {$aliasName}.room_id = 23"
//" AND {$aliasName}.id = 1420"
" FOR UPDATE"
);
if (empty($result)) {
return $this->_stop();
}
$totalCount = $result[0][0]['count_num'];
$maxLoop = ceil($totalCount / self::PROCESS_COUNT);
$procCount = 0;
for ($i = 0; $i < $maxLoop; $i++) {
//ログ出力
RoomsLibLog::processStartLog(
$this,
sprintf('Delete Related Tables[%s]', ($i * self::PROCESS_COUNT + 1) . '/' . $totalCount)
);
$records = $this->RoomDeleteRelatedTable->query(
"SELECT {$aliasName}.* FROM {$tableName} AS {$aliasName}" .
" WHERE {$aliasName}.end_time IS NULL" .
//" AND {$aliasName}.room_id = 23"
//" AND {$aliasName}.id = 1420" .
" LIMIT " . self::PROCESS_COUNT
);
try {
foreach ($records as $record) {
//トランザクションBegin
$this->RoomDeleteRelatedTable->begin();
$recordId = $record['RoomDeleteRelatedTable']['id'];
$this->RoomDeleteRelatedTable->updateStartTime($recordId);
$this->__RoomsLibDeleteRoomTables->deleteRelatedTables(
$record['RoomDeleteRelatedTable']['delete_table_name'],
$record['RoomDeleteRelatedTable']['field_name'],
$record['RoomDeleteRelatedTable']['value'],
$record['RoomDeleteRelatedTable']['foreign_field_conditions']
);
$this->RoomDeleteRelatedTable->updateEndTime($recordId);
$procCount++;
//トランザクションCommit
$this->RoomDeleteRelatedTable->commit();
//$this->RoomDeleteRelatedTable->rollback();
}
} catch (Exception $ex) {
//トランザクションRollback
$this->RoomDeleteRelatedTable->rollback($ex);
}
//ログ出力
RoomsLibLog::processEndLog(
$this,
sprintf('Delete Related Tables[%s]', $procCount . '/' . $totalCount)
);
}
}
/**
* 引数の使い方の取得
*
* @return ConsoleOptionParser
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
return $parser->description(__d('rooms', 'The Delete room associations Shell'));
}
}