public function persistPackage(Package $package) { $this->persistVendor($package->getVendor()); $data = array($package->getCreatedAt()->format('Y-m-d H:i:s'), $package->getUpdatedAt()->format('Y-m-d H:i:s'), $package->getUserId(), $package->getVendor()->getId(), $package->getName(), $package->getFullname(), $package->getDescription(), $package->getRepositoryUrl(), $package->getRepositoryType()); if ($package->getId()) { $data[] = $package->getId(); $sql = "UPDATE " . $this->getTablePrefix() . "package\n SET\n\t\t\t\t\t\tcreated_at = ?,\n\t\t\t\t\t\tupdated_at = ?,\n\t\t\t\t\t\tuser_id = ?,\n\t\t\t\t\t\tvendor_id = ?,\n\t\t\t\t\t\tname = ?,\n fullname = ?,\n description = ?,\n repository_url = ?,\n repository_type = ?\n WHERE\n id = ?"; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); } else { $sql = "INSERT INTO " . $this->getTablePrefix() . "package\n (created_at, updated_at, user_id, vendor_id, name, fullname, description, repository_url, repository_type)\n VALUES\n (?, ?, ?, ?, ?, ?, ?, ?, ?)"; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); $package->setId($this->pdo->lastInsertId()); } // Persist the versions: $persistedVersions = array(); foreach ($package->getVersions() as $version) { $this->persistVersion($version); $persistedVersions[] = $version->getId(); } // Remove all versions that were not persisted: $sql = "DELETE FROM " . $this->getTablePrefix() . "version WHERE package_id = ? AND id NOT IN (" . implode(', ', $persistedVersions) . ")"; $stmt = $this->pdo->prepare($sql); $stmt->execute(array($package->getId())); }