public function saveFollowing(Following $following, $user_id) { $id = (int) $following->getId(); if (!$id) { $following->setUserId($user_id); } $data = $following->getData(); if ($id == 0) { $query = sprintf("\n INSERT INTO %s\n ({$this->quoteColumnName('userId')}, {$this->quoteColumnName('followingName')}, {$this->quoteColumnName('followingId')})\n VALUES (:user_id, :following_name, :following_id)", self::TABLE_NAME); $stmt = $this->dbAdapter->prepare($query); $stmt->bindValue(':user_id', $data['userId'], \PDO::PARAM_INT); $stmt->bindValue(':following_name', $data['followingName'], \PDO::PARAM_STR); $stmt->bindValue(':following_id', $data['followingId'], \PDO::PARAM_INT); $stmt->execute(); } else { if ($this->getUserFollowing($id, $user_id)) { $query = sprintf("\n UPDATE %s\n SET {$this->quoteColumnName('userId')} = :user_id,\n {$this->quoteColumnName('followingName')} = :following_name,\n {$this->quoteColumnName('followingId')} = :following_id\n WHERE id = :id", self::TABLE_NAME); $stmt = $this->dbAdapter->prepare($query); $stmt->bindValue(':user_id', $data['userId'], \PDO::PARAM_INT); $stmt->bindValue(':following_name', $data['followingName'], \PDO::PARAM_STR); $stmt->bindValue(':following_id', $data['followingId'], \PDO::PARAM_INT); $stmt->bindValue(':id', $id, \PDO::PARAM_INT); $stmt->execute(); } else { throw new \Exception('Following does not exist'); } } }
protected function _populateItems(array $items) { foreach ($items as $item_data) { if (is_array($item_data)) { $item = new Following(); $item->exchangeArray($item_data); $this->add($item); } } }