/** * Saves a link into the database. * * $param \WebLinks\Domain\Link $link The link to save */ public function save(Link $link) { $linkData = array('link_title' => $link->getTitle(), 'link_url' => $link->getUrl(), 'user_id' => $link->getAuthor()->getId()); if ($link->getId()) { // The link has already been saved : update it $this->getDb()->update('t_link', $linkData, array('link_id' => $link->getId())); } else { // The link has never been saved : insert it $this->getDb()->insert('t_link', $linkData); // Get the id of the newly created link and set it on the entity $id = $this->getDb()->lastInsertId(); $link->setId($id); } }
/** * Creates an Link object based on a DB row. * * @param array $row The DB row containing Link data. * @return \WebLinks\Domain\Link */ protected function buildDomainObject($row) { $link = new Link(); $link->setId($row['link_id']); $link->setUrl($row['link_url']); $link->setTitle($row['link_title']); if (array_key_exists('user_id', $row)) { // Find and set the associated author $userId = $row['user_id']; $user = $this->userDAO->find($userId); $link->setAuthor($user); } return $link; }