Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added workaround for duplicate key error
Since we're processing additional data during registration, we need to check if these data were defined in db to be unique. 
For example, email addresses are usually used just once in an application. We can query the database to check if the new email address is not yet registered, but, in some cases, we may more than 2 or 3 or more unique fields (not common, but possible), hence we would also need to query 2,3 or more times. 

As a TEMPORARY WORKAROUND, we'll just attempt to register the new user and wait for the db to throw a DUPLICATE KEY EXCEPTION.
  • Loading branch information
apps-caraga authored Sep 17, 2022
commit d5c63a6b1ecdb15025c29e04f21a75cca2d3222c
16 changes: 15 additions & 1 deletion src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,21 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
}
}
$this->db->createSingle($table, $data);
try{
$this->db->createSingle($table, $data);
/* Since we're processing additional data during registration, we need to check if these data were defined in db to be unique.
* For example, emailAddress are usually used just once in an application. We can query the database to check if the new emailAddress is not yet registered,
* but, in some cases, we may more than 2 or 3 or more unique fields (not common, but possible), hence we would also need to
* query 2,3 or more times.
* As a TEMPORARY WORKAROUND, we'll just attempt to register the new user and wait for the db to throw a DUPLICATE KEY EXCEPTION.
*/
}catch(\PDOException error){
if($error->getCode() ==="23000"){
return $this->responder->error(ErrorCode::DUPLICATE_KEY_EXCEPTION,'',$error->getMessage());
}else{
return $this->responder->error(ErrorCode::INPUT_VALIDATION_FAILED,$$error->getMessage());
}
}
$users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
foreach ($users as $user) {
unset($user[$passwordColumnName]);
Expand Down