/** * Insert new article(s) into the database. * * @param mixed $articles A single {@link OnpubArticle} object or an array of {@link OnpubArticle} objects (to insert multiple articles at a time). * @return mixed The ID(s) of the new article(s). An int will be returned if a single article was inserted. An array of ints will be returned if multiple articles were inserted. * @throws PDOException if there's a database error. */ public function insert($articles) { $oimages = new OnpubImages($this->pdo, FALSE); $oauthors = new OnpubAuthors($this->pdo, FALSE); $oaamaps = new OnpubAAMaps($this->pdo, FALSE); $osamaps = new OnpubSAMaps($this->pdo, FALSE); $IDs = array(); $isArray = TRUE; if (!is_array($articles)) { $articles = array($articles); $isArray = FALSE; } if ($this->enableTransactions) { $status = $this->pdo->beginTransaction(); OnpubDatabase::verifyTransaction($this->pdo, $status); } $stmt = $this->pdo->prepare("INSERT INTO OnpubArticles (ID, imageID, title, content, url, created, modified) VALUES (:ID, :imageID, :title, :content, :url, :created, :modified)"); OnpubDatabase::verifyPrepare($this->pdo, $stmt, $this->enableTransactions); foreach ($articles as $article) { if ($article->image) { try { $imageID = $oimages->insert($article->image); $article->imageID = $imageID; } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } try { $ID = $this->getID($article); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } if ($ID) { $IDs[] = $ID; $article->ID = $ID; } else { $ID = $article->ID; $imageID = $article->imageID; $title = OnpubDatabase::utf8Decode(trim($article->title)); $content = OnpubDatabase::utf8Decode(trim($article->content)); $url = OnpubDatabase::utf8Decode(trim($article->url)); $created = $article->getCreated()->format('Y-m-d H:i:s'); $modified = $article->getModified()->format('Y-m-d H:i:s'); $stmt->bindParam(':ID', $ID); $stmt->bindParam(':imageID', $imageID); $stmt->bindParam(':title', $title); $stmt->bindParam(':content', $content); $stmt->bindParam(':url', $url); $stmt->bindParam(':created', $created); $stmt->bindParam(':modified', $modified); $result = $stmt->execute(); OnpubDatabase::verifyExecute($this->pdo, $result, $this->enableTransactions, $stmt->errorInfo()); $IDs[] = $this->pdo->lastInsertId(); $article->ID = $this->pdo->lastInsertId(); } $authors = $article->authors; try { $oauthors->insert($authors); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } $aamaps = array(); foreach ($authors as $author) { $aamap = new OnpubAAMap(); $aamap->articleID = $article->ID; $aamap->authorID = $author->ID; $aamap->setCreated($article->getCreated()); $aamap->setModified($article->getModified()); $aamaps[] = $aamap; } try { $oaamaps->insert($aamaps); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } $sectionIDs = $article->sectionIDs; $samaps = array(); foreach ($sectionIDs as $sectionID) { $samap = new OnpubSAMap(); $samap->sectionID = $sectionID; $samap->articleID = $article->ID; $samap->setCreated($article->getCreated()); $samap->setModified($article->getModified()); $samaps[] = $samap; } try { $osamaps->insert($samaps); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } if ($this->enableTransactions) { $status = $this->pdo->commit(); OnpubDatabase::verifyTransaction($this->pdo, $status); } if ($isArray) { return $IDs; } else { return end($IDs); } }
/** * @param OnpubQueryOptions $queryOptions Database query options. * @return array An array of {@link OnpubAAMap} objects. */ public function select(OnpubQueryOptions $queryOptions = NULL) { if ($queryOptions === NULL) { $queryOptions = new OnpubQueryOptions(); } $query = $this->selectQuery($queryOptions); $result = $this->pdo->query($query); OnpubDatabase::verifyQuery($this->pdo, $result, FALSE); $rows = $result->fetchAll(PDO::FETCH_ASSOC); $aamaps = array(); if ($rows) { foreach ($rows as $row) { $aamap = new OnpubAAMap(); $aamap->ID = $row["ID"]; $aamap->articleID = $row["articleID"]; $aamap->authorID = $row["authorID"]; $aamap->setCreated(new DateTime($row["created"])); $aamap->setModified(new DateTime($row["modified"])); $aamaps[] = $aamap; } } $result->closeCursor(); return $aamaps; }