public static function addUserData($userData) { $query = "INSERT INTO UserData (userId, user_name, skill_level, \r\n\t\t\t\tprofile_pic, started_hobby, fav_color, url, phone) VALUES\r\n\t\t\t\t(:userId, :user_name, :skill_level, :profile_pic,\r\n\t\t\t\t:started_hobby, :fav_color, :url, :phone)"; // TODO: Add a functionalized RobotData INSERT query // TODO: Functionalize the SkillAssoc INSERT query try { // check null and check for errors // check for User by given userId; throw exception if non-existent if (is_null($userData) || $userData->getErrorCount() > 0) { throw new PDOException("Invalid UserData object can't be inserted: "); } $newUserId = $userData->getUserId(); if (!isset($newUserId)) { throw new PDOException("UserId not specified"); } // Verify that the specified user exists in the database $db = Database::getDB(); $users = UsersDB::getUserValuesBy('userId', $newUserId, 'userId'); if ($users[0] != $newUserId) { throw new PDOException("Cannot associate UserData with invalid User"); } $statement = $db->prepare($query); $statement->bindValue(":userId", $newUserId); $statement->bindValue(":user_name", $userData->getUserName()); $statement->bindValue(":skill_level", $userData->getSkillLevel()); // TODO: Have the profile pic uploaded to a designated folder and moved $statement->bindValue(":profile_pic", $userData->getProfilePic()); $statement->bindValue(":started_hobby", $userData->getStartedHobby()); $statement->bindValue(":fav_color", $userData->getFavColor()); $statement->bindValue(":url", $userData->getUrl()); $statement->bindValue(":phone", $userData->getPhone()); $statement->execute(); $statement->closeCursor(); $returnId = $db->lastInsertId("userDataId"); // Handle skill area associations separately since they're going into a different table $query = "INSERT INTO SkillAssocs (userDataId, skillId) VALUES\r\n\t\t\t\t\t(:userDataId, :skillId)"; // TODO: Review this for instances where this can go wrong foreach ($userData->getSkillAreas() as $skill) { // Translate the skill (a string) to a skillId (an integer), // then create a skill association $skillArray = SkillsDB::getSkillsBy('skill_name', $skill); $skillObject = $skillArray[0]; $skillstatement = $db->prepare($query); $skillstatement->bindValue(":userDataId", $returnId); $skillstatement->bindValue(":skillId", $skillObject->getSkillId()); $skillstatement->execute(); $skillstatement->closeCursor(); $skillAssocId = $db->lastInsertId("skillAssocId"); } $userData->setUserDataId($returnId); } catch (Exception $e) { // Not permanent error handling $userData->setError('userDataId', 'USER_DATA_INVALID'); } return $userData; }
$users = UsersDB::getUsersBy('userId', '3'); echo "The value of User 3 is:<br>{$users['0']}<br>"; ?> <h2>It should not get a User not in Users</h2> <?php $users = UsersDB::getUsersBy('userName', 'Alfred'); if (empty($users)) { echo "No User Alfred"; } else { echo "The value of User Alfred is:<br>{$users['0']}<br>"; } ?> <h2>It should not get a User by a field that isn't there</h2> <?php $users = UsersDB::getUsersBy('telephone', '21052348234'); if (empty($users)) { echo "No User with this telephone number"; } else { echo "The value of User with a specified telephone number is:<br>{$user}<br>"; } ?> <h2>It should get a user name by user id</h2> <?php $userNames = UsersDB::getUserValuesBy('userId', 1, 'userName'); print_r($userNames); ?> </body> </html>
Database::clearDB(); $db = Database::getDB('ptest'); $users = UsersDB::getUsersBy('telephone', '21052348234'); if (empty($users)) { echo "No User with this telephone number"; } else { echo "The value of User with a specified telephone number is:<br>{$user}<br>"; } ?> <h2>It should get a user name by user id</h2> <?php DBMaker::create('ptest'); Database::clearDB(); $db = Database::getDB('ptest'); $userNames = UsersDB::getUserValuesBy('userName', 'userID', 1); print_r($userNames); ?> <h2>It should allow update of the user name</h2> <?php DBMaker::create('ptest'); Database::clearDB(); $db = Database::getDB('ptest'); $users = UsersDB::getUsersBy('userID', 1); $user = $users[0]; echo "<br>Before update: {$user} <br>"; $parms = $user->getParameters(); $parms['userName'] = '******'; $newUser = new User($parms); $newUser->setUserID(1); $user = UsersDB::updateUser($newUser);