/** * Creates a new user message and adds it to the message que db * * @param string $userId * @param string $subject * @param string $body * @param string $level * @param string $source */ public function createUserMessage($userId, $subject, $body, $level, $source) { $message = new Message(); $message->setSubject($subject); $message->setMessage($body); $message->setLevel($level); $message->setSource($source); $this->entityMgr->persist($message); $this->entityMgr->flush($message); $userMessage = new UserMessage($userId); $userMessage->setMessage($message); $this->entityMgr->persist($userMessage); $this->entityMgr->flush($userMessage); }
public function testSetGet() { $now = new \DateTime(); $testCase = ['id' => 123, 'userId' => 4444, 'dateViewed' => $now, 'message' => 'TEST MESSAGE', 'dateViewed' => $now, 'dateViewedString' => $now->format(\DateTime::ISO8601)]; $unit = new UserMessage(321); $unit->setId($testCase['id']); $this->assertEquals($testCase['id'], $unit->getId()); $this->assertEquals(321, $unit->getUserId()); $unit->setUserId($testCase['userId']); $this->assertEquals($testCase['userId'], $unit->getUserId()); $unit->setMessage($testCase['message']); $this->assertEquals($testCase['message'], $unit->getMessage()); $unit->setViewed(); $this->assertTrue($unit->hasViewed()); $dateViewed = $unit->getDateViewed(); $this->assertInstanceOf('\\DateTime', $dateViewed); $unit->setDateViewed(new \DateTime()); // still the same $this->assertInstanceOf('\\DateTime', $dateViewed); $this->assertEquals($dateViewed->format(\DateTime::ISO8601), $unit->getDateViewedString()); $unit->setViewed(false); $this->assertFalse($unit->hasViewed()); $this->assertNull($unit->getDateViewedString()); $unit->setDateViewedString($testCase['dateViewedString']); $this->assertEquals($testCase['dateViewedString'], $unit->getDateViewedString()); $this->assertTrue(is_array($unit->toArray())); $this->assertEquals($testCase['id'], $unit->toArray()['id']); }
/** * create * * @param mixed $data * * @return ApiJsonModel|\Zend\Stdlib\ResponseInterface */ public function create($data) { if (!$this->rcmIsAllowed('sites', 'admin')) { $this->getResponse()->setStatusCode(Response::STATUS_CODE_401); return $this->getResponse(); } $userId = $this->getEvent()->getRouteMatch()->getParam('userId', null); $newUserMessage = new UserMessage($userId); $newUserMessage->populate($data, ['id', 'dateViewed', 'message']); // @todo Should we force creation???? $newMessage = new Message(); $newMessage->populate($data['message'], ['id', 'dateCreated']); $newUserMessage->setMessage($newMessage); $entityManager = $this->getEntityManager(); try { $entityManager->persist($newUserMessage); $entityManager->flush(); } catch (\Exception $e) { return new ApiJsonModel(null, 1, $e->getMessage()); } return new ApiJsonModel($newUserMessage); }