/** * Saves the phone details to the database. * * @param \PhoneDirectory\Entity\PhoneBook $phoneBook */ public function save($phoneBook) { $phoneBookObj = array('name' => $phoneBook->getName(), 'phone_number' => $phoneBook->getPhoneNumber(), 'additional_notes' => $phoneBook->getAdditionalNotes()); if (!$phoneBook->getId()) { $phoneBookObj['date_created'] = date("d M Y"); $phoneBookObj['date_updated'] = date("Y-m-d H:i:s"); $this->db->insert('phone_details', $phoneBookObj); // Get the id of the newly created phone record and set it on the entity. $id = $this->db->lastInsertId(); $phoneBook->setId($id); } else { $phoneBookObj['date_updated'] = date("Y-m-d H:i:s"); $this->db->update('phone_details', $phoneBookObj, array('id' => $phoneBook->getId())); } }
public function addUpdateAction(Request $request, Application $app) { $data = UtilsService::checkJsonStructure($request); if ($data === -1 || $data === NULL) { return UtilsService::createAndSendResponse($app, array(ResponseMessagesAndStatuses::FATAL_ERROR_STATUS_CODE, ResponseMessagesAndStatuses::JSON_FORMAT_WRONG_MESSAGE)); } $checkMissingDataOrSendResponse = UtilsService::checkRequestParamsMissing($data, array('name', 'phone_number'), $app); if ($checkMissingDataOrSendResponse !== -1) { return $checkMissingDataOrSendResponse; } $phoneObj = new PhoneBook(); if (isset($data["id"])) { $phoneObj->setId($data["id"]); } $phoneObj->setName($data['name']); $phoneObj->setPhoneNumber($data['phone_number']); $phoneObj->setAdditionalNotes($data['additional_notes']); try { $app['repository.phone_details']->save($phoneObj); } catch (\Exception $e) { return UtilsService::createAndSendResponse($app, array(ResponseMessagesAndStatuses::FATAL_ERROR_STATUS_CODE, $e->getMessage())); } return UtilsService::createAndSendResponse($app, array(ResponseMessagesAndStatuses::CREATE_UPDATED_SUCCESS_STATUS_CODE, ResponseMessagesAndStatuses::CREATED_UPDATED_ROW_MESSAGE)); }