public function executeProcessForm(sfWebRequest $request) { ini_set('display_errors', false); $data = array(); $util = new Util(); $data = $request->getPostParameter("data"); $q = Doctrine_Query::create()->update('Movie')->set('name_org', '?', $data['name_org'])->set('director', '?', $data['director'])->set('rating', '?', $data['rating'])->set('plot', '?', $data['plot'])->where('id = ?', $data['id']); $rows = $q->execute(); $movie = Doctrine::getTable('Movie')->findOneById($data['id']); //year if ($data['year'] == "") { $movie->Year = NULL; $movie->save(); } else { $year_object = Doctrine::getTable('Year')->findOneByName($data['year']); if ($year_object == NULL) { $year_object = new Year(); $year_object->name = $data['year']; $year_object->coverMo = $util->changePathSeparators($util->makePath(array(sfConfig::get('app_years_rel'), '_' . $util->unformatName($data['year']) . '.jpg'))); $year_object->coverMu = $util->changePathSeparators($util->makePath(array(sfConfig::get('app_years_rel'), $util->unformatName($data['year']) . '.jpg'))); $year_object->save(); } $movie->Year = $year_object; $movie->save(); } //country if ($data['country'] == "") { $movie->Country = NULL; $movie->save(); } else { $country_object = Doctrine::getTable('Country')->findOneByName($data['country']); if ($country_object == NULL) { $country_object = new Country(); $country_object->name = $data['country']; $country_object->cover = $util->changePathSeparators($util->makePath(array(sfConfig::get('app_countries_rel'), $util->unformatName($data['country']) . '.png'))); $country_object->save(); } $movie->Country = $country_object; $movie->save(); } //genres foreach ($data['genres'] as $genre) { $genre_object = Doctrine::getTable('Genre')->findOneByName($genre); //if this not exist let's create it if ($genre_object == NULL) { $genre_object = new Genre(); $genre_object->name = $genre; $genre_object->cover = $util->changePathSeparators($util->makePath(array(sfConfig::get('app_genres_rel'), $util->unformatName($genre) . '.jpg'))); $genre_object->Movies[] = $movie; $genre_object->save(); } else { //check if the movie already has such Genre $found = false; foreach ($movie['MovieGenre'] as $db_genre) { if ($db_genre['Genre']['name'] == $genre) { $found = true; } } if ($found == false) { $genre_object->Movies[] = $movie; $genre_object->save(); } } } //removing the db genres which are not in the request foreach ($movie['MovieGenre'] as $db_genre) { $found2 = false; foreach ($data['genres'] as $genre) { if ($db_genre['Genre']['name'] == $genre) { $found2 = true; } } if ($found2 == false) { $q = Doctrine_Query::create()->delete('MovieGenre')->addWhere('movie_id = ?', $db_genre['movie_id'])->andWhere('genre_id = ?', $db_genre['genre_id']); $deleted = $q->execute(); } } //actors foreach ($data['actors'] as $item) { $actor_object = Doctrine::getTable('Actor')->findOneByName($item['actor']); //if this not exist let's create it if ($actor_object == NULL) { $actor_object = new Actor(); $actor_object->name = $item['actor']; $actor_object->cover = $util->changePathSeparators($util->makePath(array(sfConfig::get('app_actors_rel'), $util->unformatName($item['actor']) . '.jpg'))); $actor_object->Movies[] = $movie; $actor_object->save(); $q = Doctrine_Query::create()->from('MovieActor ma')->where('ma.movie_id = ?', $movie['id'])->andWhere('ma.actor_id = ?', $actor_object['id']); $movie_actor = $q->execute(); $movie_actor[0]->role = $item['character']; $movie_actor[0]->save(); } else { //check if the movie already has such Actor $found = false; foreach ($movie['MovieActor'] as $db_actor) { if ($db_actor['Actor']['name'] == $item['actor']) { $found = true; } } if ($found == false) { $movie_actor = new MovieActor(); $movie_actor->actor_id = $actor_object['id']; $movie_actor->movie_id = $movie['id']; $movie_actor->role = $item['character']; $movie_actor->save(); } } } //removing the db genres which are not in the request foreach ($movie['MovieActor'] as $db_actor) { $found2 = false; foreach ($data['actors'] as $item) { if ($db_actor['Actor']['name'] == $item['actor']) { $found2 = true; } } if ($found2 == false) { $q = Doctrine_Query::create()->delete('MovieActor')->addWhere('movie_id = ?', $db_actor['movie_id'])->andWhere('actor_id = ?', $db_actor['actor_id']); } $deleted = $q->execute(); } //var_dump($data); exit; }
/** * Updates the movie in database getting data from form. * Used in /movie/actions/executeRefreshMovieCollection * * @param Array $movie_info[] ex: [Star Wars, USA, 2005, [Comedy, Sci-Fi]] */ public static function updateMovieFromForm($movie_info) { $movie_object = Doctrine::getTable('Movie')->findOneById($movie_info['id']); //if there is no movie then raise an error if ($movie_object == NULL) { return 1; } $movie_object->org_title = $movie_info['org_title']; $movie_object->rating = $movie_info['rating']; $movie_object->plot = $movie_info['plot']; /********************************* YEAR *********************************/ if ($movie_info['year'] == "") { $movie_object->Year = NULL; } else { $year_object = Doctrine::getTable('Year')->findOneByName($movie_info['year']); if ($year_object == NULL) { $year_object = Util::createYear($movie_info['year']); } $movie_object->Year = $year_object; } /******************************* COUNTRIES *******************************/ foreach ($movie_info['countries'] as $country) { $country_object = Doctrine::getTable('Country')->findOneByName($country); //if this not exist let's create it if ($country_object == NULL) { $country_object = Util::createCountry($country); $movie_object->Countries[] = $country_object; } else { //check if the movie already has such director $found = false; foreach ($movie_object['Countries'] as $db_country) { if ($db_country['name'] == $country) { $found = true; } } if ($found == false) { $movie_object->Countries[] = $country_object; } } } //removing the db countries which are not in the request foreach ($movie_object['MovieCountry'] as $db_country) { $found2 = false; foreach ($movie_info['countries'] as $country) { if ($db_country['Country']['name'] == $country) { $found2 = true; } } if ($found2 == false) { Util::removeCountryFromMovie($db_country); } } /******************************* DIRECTORS *******************************/ foreach ($movie_info['directors'] as $director) { $director_object = Doctrine::getTable('Director')->findOneByName($director); //if this not exist let's create it if ($director_object == NULL) { $director_object = Util::createDirector($director); $movie_object->Directors[] = $director_object; } else { //check if the movie already has such director $found = false; foreach ($movie_object['MovieDirector'] as $db_director) { if ($db_director['Director']['name'] == $director) { $found = true; } } if ($found == false) { $movie_object->Directors[] = $director_object; } } } //removing the db genres which are not in the request foreach ($movie_object['MovieDirector'] as $db_director) { $found2 = false; foreach ($movie_info['directors'] as $director) { if ($db_director['Director']['name'] == $director) { $found2 = true; } } if ($found2 == false) { Util::removeDirectorFromMovie($db_director); } } /********************************* GENRES *********************************/ foreach ($movie_info['genres'] as $genre) { $genre_object = Doctrine::getTable('Genre')->findOneByName($genre); //if this not exist let's create it if ($genre_object == NULL) { $genre_object = Util::createGenre($genre); $movie_object->Genres[] = $genre_object; } else { //check if the movie already has such Genre $found = false; foreach ($movie_object['Genres'] as $db_genre) { if ($db_genre['name'] == $genre) { $found = true; } } if ($found == false) { $movie_object->Genres[] = $genre_object; } } } //removing the db genres which are not in the request foreach ($movie_object['MovieGenre'] as $db_genre) { $found2 = false; foreach ($movie_info['genres'] as $genre) { if ($db_genre['Genre']['name'] == $genre) { $found2 = true; } } if ($found2 == false) { Util::removeGenreFromMovie($db_genre); } } /********************************* ACTORS *********************************/ foreach ($movie_info['actors'] as $item) { $actor_object = Doctrine::getTable('Actor')->findOneByName($item['actor']); //if this not exist let's create it if ($actor_object == NULL) { echo 'creating: ' . $item['actor'] . '<br>'; $actor_object = Util::createActorInMovie($movie_object, $item); } else { //check if this actor has already the same relation to movie $q = Doctrine_Query::create()->from('MovieActor ma')->where('ma.movie_id = ?', $movie_object['id'])->andWhere('ma.actor_id = ?', $actor_object['id']); $rows = $q->execute(); if (count($rows) == 0) { echo 'adding link to a member that is already on db: ' . $item['actor'] . '<br>'; $movie_actor = new MovieActor(); $movie_actor->actor_id = $actor_object['id']; $movie_actor->movie_id = $movie_object['id']; $movie_actor->role = $item['character']; $movie_actor->save(); } else { //update role echo 'updating link of a member that is already on db: ' . $item['actor'] . '<br>'; $q = Doctrine_Query::create()->update('MovieActor ma')->set('ma.role', '?', $item['character'])->where('ma.movie_id = ?', $movie_object['id'])->andWhere('ma.actor_id = ?', $actor_object['id'])->execute(); } } } //removing the db actors which are not in the request foreach ($movie_object['MovieActor'] as $db_actor) { $found2 = false; foreach ($movie_info['actors'] as $item) { if ($db_actor['Actor']['name'] == $item['actor']) { $found2 = true; } } //actor from form array is in db, let's update its role // if ($found2 == true){ // echo 'adding link to db member: ' . $db_actor['Actor']['name'] .'<br>'; // $q = Doctrine_Query::create() // ->update('MovieActor ma') // ->set('ma.role', '?', $char) // ->where('ma.movie_id = ?', $movie_object['id']) // ->andWhere('ma.actor_id = ?', $db_actor['Actor']['id']) // ->execute(); // } //if not, unset link between movie and actor if ($found2 == false) { echo 'deleting link: ' . $db_actor['Actor']['name'] . '<br>'; Util::removeActorFromMovie($movie_object, $db_actor); } } $movie_object->save(); return 0; }