public function add($title, $year, $format, $names, $surnames) { $movieid = db::getNewMovieId(); $table_movies = new Table('movie', array('id', 'title', 'year')); $table_actors = new Table('actor', array('movieid', 'name', 'surname')); $table_movie_formats = new Table('movie_format', array('movieid', 'format')); $table_movies->addDataRow(array($movieid, $title, $year)); $table_movie_formats->addDataRow(array($movieid, $format)); foreach ($names as $key => $name) { $table_actors->addDataRow(array($movieid, $name, $surnames[$key])); } Table::fillMysqlTables(array($table_movies, $table_actors, $table_movie_formats), $this->db); }
private function parseRows($file) { //read using generators $handle = fopen($file['tmp_name'], "r"); if ($handle) { $table_movies = new Table('movie', array('id', 'title', 'year')); $table_actors = new Table('actor', array('movieid', 'name', 'surname')); $table_movie_formats = new Table('movie_format', array('movieid', 'format')); $title = $this->getCheckedRow(fgets($handle)); $movieid = db::getNewMovieId(); while (!empty($title)) { $year = $this->getCheckedRow(fgets($handle)); $format = $this->getCheckedRow(fgets($handle)); $actors = $this->getCheckedRow(fgets($handle)); $table_movies->addDataRow(array($movieid, $title, $year)); $table_movie_formats->addDataRow(array($movieid, $format)); $actors_arr = explode(',', $actors); foreach ($actors_arr as $actorInitials) { $actorInitialsKV = explode(' ', trim($actorInitials)); $table_actors->addDataRow(array($movieid, $actorInitialsKV[0], $actorInitialsKV[1])); } fgets($handle); $title = $this->getCheckedRow(fgets($handle)); ++$movieid; //return, if input data has wrong format if (!$this->result['success']) { fclose($handle); return; } } fclose($handle); //try to load data into SQL table $transaction_result = Table::fillMysqlTables(array($table_movies, $table_actors, $table_movie_formats), $this->db); if ($transaction_result === false) { $this->setFailResult('Wrong data format - data could not be loaded'); } } else { $this->setFailResult('Error - file could not be opened'); } }