/** * Adds a fixture to the collection. * * @param Fixture $fixture * @return $this * @throws \TheIconic\Fixtures\Exception\FixtureException */ public function add(Fixture $fixture) { if (isset($this->fixtures[$fixture->getName()])) { throw new FixtureException('Fixture ' . $fixture->getName() . ' already defined'); } else { $this->fixtures[$fixture->getName()] = $fixture; } return $this; }
/** * {@inheritDoc} */ public function replaceValues(Fixture $fixture, array $replacementPlaceholders) { // Only attempt to replace values when fixture is marked for it if (array_key_exists($fixture->getName(), $replacementPlaceholders)) { $replacementPlaceholders = $replacementPlaceholders[$fixture->getName()]; $replacedData = $this->replacePlaceholders($fixture, $replacementPlaceholders); $fixture->setData($replacedData); } return $fixture; }
/** * {@inheritDoc} Cleans database table before doing so. * * @param Fixture $fixture * @return bool */ public function persist(Fixture $fixture) { $database = $this->config['database']; $table = $fixture->getName(); $this->getConnection()->query("SET FOREIGN_KEY_CHECKS = 0;"); $this->getConnection()->query("TRUNCATE `{$database}`.`{$table}`;"); $success = true; foreach ($fixture as $fixtureData) { $columns = array_keys($fixtureData); $columns = implode('`, `', $columns); $values = []; foreach ($fixtureData as $attribute => $value) { $values[':' . $attribute] = $value; } $placeholders = implode(", ", array_keys($values)); $sql = "INSERT INTO `{$database}`.`{$table}` (`{$columns}`) VALUES({$placeholders});"; $pdoStatement = $this->getConnection()->prepare($sql); try { $success = $success && $pdoStatement->execute($values); } catch (\PDOException $e) { throw new PersisterException("ERROR with fixture '{$table}': " . $e->getMessage()); } } $this->getConnection()->query("SET FOREIGN_KEY_CHECKS = 1;"); return $success; }
/** * {@inheritDoc} Cleans database table before doing so. * * @param Fixture $fixture * @return bool */ public function persist(Fixture $fixture) { $fixtureName = $fixture->getName(); return $this->getConnection()->set($fixtureName, $fixture->getIterator()->getArrayCopy()); }