/** * @param mixed $sections A single {@link OnpubSection} object or an array of {@link OnpubSection} objects (to insert multiple sections at a time). * @return mixed The ID(s) of the new section(s). An int will be returned if a single section was inserted. An array of ints will be returned if multiple sections were inserted. * @throws PDOException if there's a database error. */ public function insert($sections, $insertWSMaps = FALSE) { $oimages = new OnpubImages($this->pdo, FALSE); $owsmaps = new OnpubWSMaps($this->pdo, FALSE); $IDs = array(); $isArray = TRUE; $wsmaps = array(); if (!is_array($sections)) { $sections = array($sections); $isArray = FALSE; } if ($this->enableTransactions) { $status = $this->pdo->beginTransaction(); OnpubDatabase::verifyTransaction($this->pdo, $status); } $stmt = $this->pdo->prepare("INSERT INTO OnpubSections (ID, imageID, websiteID, parentID, name, url, created, modified) VALUES (:ID, :imageID, :websiteID, :parentID, :name, :url, :created, :modified)"); OnpubDatabase::verifyPrepare($this->pdo, $stmt, $this->enableTransactions); foreach ($sections as $section) { if ($section->image) { try { $imageID = $oimages->insert($section->image); $section->imageID = $imageID; } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } try { $ID = $this->getID($section); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } if ($ID) { $IDs[] = $ID; $section->ID = $ID; } else { $ID = $section->ID; $imageID = $section->imageID; $websiteID = $section->websiteID; $parentID = $section->parentID; $name = OnpubDatabase::utf8Decode(trim($section->name)); $url = OnpubDatabase::utf8Decode(trim($section->url)); $created = $section->getCreated()->format('Y-m-d H:i:s'); $modified = $section->getModified()->format('Y-m-d H:i:s'); $stmt->bindParam(':ID', $ID); $stmt->bindParam(':imageID', $imageID); $stmt->bindParam(':websiteID', $websiteID); $stmt->bindParam(':parentID', $parentID); $stmt->bindParam(':name', $name); $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(); $section->ID = $this->pdo->lastInsertId(); } $wsmap = new OnpubWSMap(); $wsmap->websiteID = $section->websiteID; $wsmap->sectionID = $section->ID; $wsmap->setCreated($section->getCreated()); $wsmap->setModified($section->getModified()); $wsmaps[] = $wsmap; } if ($insertWSMaps) { try { $owsmaps->insert($wsmaps); } 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 OnpubWSMap} objects. */ public function select(OnpubQueryOptions $queryOptions = NULL, $websiteID = NULL, $sectionID = NULL) { if ($queryOptions === NULL) { $queryOptions = new OnpubQueryOptions(); } $query = $this->selectQuery($queryOptions, $websiteID, $sectionID); $result = $this->pdo->query($query); OnpubDatabase::verifyQuery($this->pdo, $result, FALSE); $rows = $result->fetchAll(PDO::FETCH_ASSOC); $wsmaps = array(); if ($rows) { foreach ($rows as $row) { $wsmap = new OnpubWSMap(); $wsmap->ID = $row["ID"]; $wsmap->websiteID = $row["websiteID"]; $wsmap->sectionID = $row["sectionID"]; $wsmap->setCreated(new DateTime($row["created"])); $wsmap->setModified(new DateTime($row["modified"])); $wsmaps[] = $wsmap; } } $result->closeCursor(); return $wsmaps; }