public function duplicateMovie($id, $idUser) { $movie = null; $resultats = $this->bdd->prepare("SELECT * FROM movie WHERE mov_id=?;"); $resultats->execute(array($id)); $resultats->setFetchMode(PDO::FETCH_OBJ); while ($result = $resultats->fetch()) { $movie = new Movie($result->mov_id, $result->mov_user, $result->mov_title, $result->mov_description_short, $result->mov_description_long, $result->mov_director, $result->mov_year, $result->mov_image, $result->mov_cat); } $movie->setUser($idUser); $req2 = $this->bdd->prepare("INSERT INTO movie (mov_title, mov_user,mov_description_short, mov_description_long, mov_director, mov_year, mov_image, mov_cat) VALUES (:titre, :utilisateur, :resume, :synopsis, :realisateur, :annee, :affiche, :categorie)"); $result2 = $req2->execute(array("titre" => htmlspecialchars($movie->getTitle()), "utilisateur" => htmlspecialchars($movie->getUser()), "resume" => htmlspecialchars($movie->getDescriptionShort()), "synopsis" => htmlspecialchars($movie->getDescriptionLong()), "realisateur" => htmlspecialchars($movie->getDirector()), "annee" => htmlspecialchars($movie->getYear()), "affiche" => htmlspecialchars($movie->getImage()), "categorie" => htmlspecialchars($movie->getCategory()))); $movie->setId($this->bdd->lastInsertId()); return $movie; }
public function updateMovie(Movie $movie) { $this->connect(); // disable auto commit, so that we can roll back bridge table deletes if // the transaction fails in the second portion of the query mysqli_autocommit($this->link, FALSE); // first, we delete all records in the actor bridge table for the movie $sqlDelete = "DELETE FROM actor WHERE movie_id = " . $movie->getId(); try { if (mysqli_query($this->link, $sqlDelete)) { // now that the bridge table is cleared out, update the movie $movieId = $movie->getId(); $directorId = $movie->getDirector()->getId(); $title = "'" . mysqli_real_escape_string($this->link, $movie->getTitle()) . "'"; $releaseDate = "'" . $movie->getReleaseDate() . "'"; $synopsis = "'" . mysqli_real_escape_string($this->link, $movie->getSynopsis()) . "'"; $sqlUpdate = "UPDATE movie " . "SET director_id = {$directorId}, " . "title = {$title}, " . "release_date = {$releaseDate}, " . "submit_date = CURDATE(), " . "synopsis = {$synopsis} " . "WHERE id = {$movieId}"; if (mysqli_query($this->link, $sqlUpdate)) { // alright, movie table is updated, now to re-enter the new // actor list into the bridge table. $callback = function ($person) use($movieId) { if ($person instanceof Person) { $personId = $person->getId(); return "({$movieId}, {$personId})"; } else { return ""; } }; $bridgePairs = array_map($callback, $movie->getActors()); $values = implode(", ", $bridgePairs); $bridgeSql = "INSERT INTO actor (movie_id, people_id) VALUES {$values}"; $result = mysqli_query($this->link, $bridgeSql); if ($result) { // ok, movie is update, and bridge table too // now we can safely commit mysqli_commit($this->link); $this->disconnect(); return TRUE; } } else { // uh oh, something went wrong, roll back and abort! mysqli_rollback($this->link); $this->disconnect(); return FALSE; } } else { return FALSE; } } catch (Exception $e) { mysqli_rollback($this->link); $this->disconnect(); return FALSE; } }