DebugController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Controllers;
  3. use App\Controllers\BaseController;
  4. use App\Models\VendorModel;
  5. use App\Models\UserModel;
  6. use App\Models\VendorInfluencerMappingModel;
  7. use CodeIgniter\HTTP\ResponseInterface;
  8. class DebugController extends BaseController
  9. {
  10. protected $vendorModel;
  11. protected $userModel;
  12. protected $vendorInfluencerModel;
  13. public function __construct()
  14. {
  15. $this->vendorModel = new VendorModel();
  16. $this->userModel = new UserModel();
  17. $this->vendorInfluencerModel = new VendorInfluencerMappingModel();
  18. }
  19. /**
  20. * 외래키 제약조건 디버깅용 메서드
  21. */
  22. public function debugForeignKey()
  23. {
  24. try {
  25. $mappingSeq = 2;
  26. $processedBy = 8;
  27. // 1. USER_LIST에서 SEQ 8번 사용자 확인
  28. $user = $this->userModel->where('SEQ', $processedBy)->first();
  29. $debugInfo = [
  30. 'user_exists' => !empty($user),
  31. 'user_data' => $user,
  32. 'user_count' => $this->userModel->where('SEQ', $processedBy)->countAllResults()
  33. ];
  34. // 2. VENDOR_INFLUENCER_MAPPING에서 SEQ 2번 레코드 확인
  35. $mapping = $this->vendorInfluencerModel->where('SEQ', $mappingSeq)->first();
  36. $debugInfo['mapping_exists'] = !empty($mapping);
  37. $debugInfo['mapping_data'] = $mapping;
  38. // 3. 현재 APPROVED_BY 필드 상태 확인
  39. if ($mapping) {
  40. $debugInfo['current_approved_by'] = $mapping['APPROVED_BY'];
  41. $debugInfo['current_status'] = $mapping['STATUS'];
  42. }
  43. // 4. 외래키 제약조건 확인
  44. $db = \Config\Database::connect();
  45. $foreignKeys = $db->query("
  46. SELECT
  47. CONSTRAINT_NAME,
  48. COLUMN_NAME,
  49. REFERENCED_TABLE_NAME,
  50. REFERENCED_COLUMN_NAME
  51. FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
  52. WHERE TABLE_NAME = 'VENDOR_INFLUENCER_MAPPING'
  53. AND TABLE_SCHEMA = DATABASE()
  54. AND REFERENCED_TABLE_NAME IS NOT NULL
  55. ")->getResultArray();
  56. $debugInfo['foreign_keys'] = $foreignKeys;
  57. // 5. 실제 업데이트 시도해보기 (트랜잭션 롤백)
  58. $db->transStart();
  59. try {
  60. $updateData = [
  61. 'STATUS' => 'APPROVED',
  62. 'APPROVED_BY' => $processedBy,
  63. 'RESPONSE_MESSAGE' => 'debug test',
  64. 'RESPONSE_DATE' => date('Y-m-d H:i:s')
  65. ];
  66. $result = $this->vendorInfluencerModel->update($mappingSeq, $updateData);
  67. $debugInfo['update_attempted'] = true;
  68. $debugInfo['update_result'] = $result;
  69. $debugInfo['update_error'] = null;
  70. } catch (\Exception $e) {
  71. $debugInfo['update_attempted'] = true;
  72. $debugInfo['update_result'] = false;
  73. $debugInfo['update_error'] = $e->getMessage();
  74. }
  75. // 항상 롤백
  76. $db->transRollback();
  77. // 6. 다른 사용자 SEQ들 확인
  78. $otherUsers = $this->userModel
  79. ->select('SEQ, NICK_NAME, EMAIL, IS_ACT, USER_TYPE')
  80. ->where('IS_ACT', 'Y')
  81. ->orderBy('SEQ')
  82. ->findAll(10);
  83. $debugInfo['sample_active_users'] = $otherUsers;
  84. return $this->response->setJSON([
  85. 'success' => true,
  86. 'debug_info' => $debugInfo
  87. ]);
  88. } catch (\Exception $e) {
  89. return $this->response->setStatusCode(500)->setJSON([
  90. 'success' => false,
  91. 'message' => '디버깅 중 오류가 발생했습니다.',
  92. 'error' => $e->getMessage()
  93. ]);
  94. }
  95. }
  96. /**
  97. * 간단한 업데이트 테스트
  98. */
  99. public function testSimpleUpdate()
  100. {
  101. try {
  102. $mappingSeq = 2;
  103. $processedBy = 8;
  104. // 직접 SQL로 업데이트 시도
  105. $db = \Config\Database::connect();
  106. $sql = "UPDATE VENDOR_INFLUENCER_MAPPING SET APPROVED_BY = ? WHERE SEQ = ?";
  107. try {
  108. $result = $db->query($sql, [$processedBy, $mappingSeq]);
  109. return $this->response->setJSON([
  110. 'success' => true,
  111. 'message' => '직접 SQL 업데이트 성공',
  112. 'affected_rows' => $db->affectedRows()
  113. ]);
  114. } catch (\Exception $e) {
  115. return $this->response->setJSON([
  116. 'success' => false,
  117. 'message' => '직접 SQL 업데이트 실패',
  118. 'error' => $e->getMessage(),
  119. 'sql_state' => $db->error()
  120. ]);
  121. }
  122. } catch (\Exception $e) {
  123. return $this->response->setStatusCode(500)->setJSON([
  124. 'success' => false,
  125. 'message' => '테스트 중 오류가 발생했습니다.',
  126. 'error' => $e->getMessage()
  127. ]);
  128. }
  129. }
  130. }