public function execute($params) { $movie = new \Engine\Movie(); $movie->setTitle($params['title']); $movie->setYear($params['year']); $movie->setFormat($params['format']); $actors = []; foreach ($params['actor_name'] as $key => $name) { $actor = new \Engine\Actor(); $actor->setName($name); $actor->setSurname($params['actor_surname'][$key]); $actors[] = $actor; } $con = \Propel\Runtime\Propel::getWriteConnection(\Engine\Map\MovieTableMap::DATABASE_NAME); $con->beginTransaction(); try { $movie->save($con); foreach ($actors as $actor) { $actor->setMovieId($movie->getId()); $actor->save($con); } $con->commit(); } catch (Exception $e) { $con->rollback(); throw $e; } return ['Message' => 'Movie has beed added successfully.', 'Status' => 1]; }
private function parseRows($file) { //read file using generators $handle = fopen($file['tmp_name'], "r"); if ($handle) { $title = $this->getCheckedRow(fgets($handle)); //read title $con = \Propel\Runtime\Propel::getWriteConnection(\Engine\Map\MovieTableMap::DATABASE_NAME); try { while (!empty($title)) { $year = $this->getCheckedRow(fgets($handle)); //read year $format = $this->getCheckedRow(fgets($handle)); //read format //create new movie $movie = new \Engine\Movie(); $movie->setTitle($title); $movie->setYear($year); $movie->setFormat($format); $movie->save($con); $actors_row = $this->getCheckedRow(fgets($handle)); // read actors $actors_arr = explode(',', $actors_row); //split line by ',' foreach ($actors_arr as $actorInitials) { $actorInitialsKV = explode(' ', trim($actorInitials)); //split actor by [name,surname] Key-Value pair //create new actor $actor = new \Engine\Actor(); $actor->setMovieId($movie->getId()); $actor->setName($actorInitialsKV[0]); $actor->setSurname($actorInitialsKV[1]); $actor->save($con); } fgets($handle); //pass whitespace //set new title $title = $this->getCheckedRow(fgets($handle)); } } catch (Exception $e) { fclose($handle); $con->rollback(); throw new \Service\X(['Message' => 'Error during database transaction', 'Fields' => '$row', 'Type' => "{$e}"]); } fclose($handle); } else { throw new \Service\X(['Message' => 'Error - file could not be opened', 'Fields' => '$handle', 'Type' => 'Stream error']); } }