/** * @inheritdoc */ public function run(ImportRow $row) { $eventDispatcher = $row->getBatch()->getContainer()->get('event_dispatcher'); $eventDispatcher->dispatch('fizz_import.before_entity_persist', new GenericEvent(array('row' => $row, 'entityAlias' => $this->entity, 'uniqueColumns' => $this->uniqueColumns))); /** @var \Doctrine\ORM\EntityManager $em */ $em = $row->getBatch()->getContainer()->get('doctrine')->getManager(); $md = $em->getClassMetadata($this->entity); $entity = null; $dataColumns = $row->getDataColumns(); if (count($this->uniqueColumns)) { $uniqueColumns = array(); foreach ($this->uniqueColumns as $column) { $uniqueColumns[$column] = $dataColumns[$column]->getValue(); } $entity = $em->getRepository($this->entity)->findOneBy($uniqueColumns); $eventDispatcher->dispatch('fizz_import.entity_persist_after_find', new GenericEvent(array('row' => $row, 'entityAlias' => $this->entity, 'entity' => $entity, 'uniqueColumns' => $this->uniqueColumns))); } if (null === $entity) { $class = $md->getName(); $entity = new $class(); $em->persist($entity); $eventDispatcher->dispatch('fizz_import.entity_persist', new GenericEvent(array('row' => $row, 'entityAlias' => $this->entity, 'entity' => $entity, 'uniqueColumns' => $this->uniqueColumns))); } foreach ($row->getDataColumns() as $dataColumn) { $md->setFieldValue($entity, $dataColumn->getColumn(), $dataColumn->getValue()); } $em->flush(); $eventDispatcher->dispatch('fizz_import.after_entity_persist', new GenericEvent(array('row' => $row, 'entityAlias' => $this->entity, 'entity' => $entity, 'uniqueColumns' => $this->uniqueColumns))); }
/** * @inheritdoc */ public function run(ImportRow $row) { $eventDispatcher = $row->getBatch()->getContainer()->get('event_dispatcher'); $eventDispatcher->dispatch('fizz_import.before_not_empty', new GenericEvent(array('row' => $row, 'column' => $this->column))); if (!array_key_exists($this->column, $dataColumns = $row->getDataColumns()) || !is_object($dataColumns[$this->column]->getValue()) && !is_array($dataColumns[$this->column]->getValue()) && !strlen($dataColumns[$this->column]->getValue())) { $row->getCommitBehaviors()->clear(); } $eventDispatcher->dispatch('fizz_import.after_not_empty', new GenericEvent(array('row' => $row, 'column' => $this->column))); }
/** * @inheritdoc */ public function run(ImportRow $row) { $eventDispatcher = $row->getBatch()->getContainer()->get('event_dispatcher'); $eventDispatcher->dispatch('fizz_import.before_datetime', new GenericEvent(array('row' => $row, 'column' => $this->column))); if (array_key_exists($this->column, $dataColumns = $row->getDataColumns())) { $dataColumns[$this->column]->setValue(new \DateTime($dataColumns[$this->column]->getValue())); } $eventDispatcher->dispatch('fizz_import.after_datetime', new GenericEvent(array('row' => $row, 'column' => $this->column))); }
/** * @inheritdoc */ public function run(ImportRow $row) { $eventDispatcher = $row->getBatch()->getContainer()->get('event_dispatcher'); $eventDispatcher->dispatch('fizz_import.before_value_to_entity', new GenericEvent(array('row' => $row, 'column' => $this->column, 'entity' => $this->entity, 'field' => $this->field))); if (array_key_exists($this->column, $dataColumns = $row->getDataColumns())) { /** @var \Doctrine\ORM\EntityManager $em */ $em = $row->getBatch()->getContainer()->get('doctrine')->getManager(); $dataColumns[$this->column]->setValue($em->getRepository($this->entity)->findOneBy(array($this->field => $dataColumns[$this->column]->getValue()))); } $eventDispatcher->dispatch('fizz_import.after_value_to_entity', new GenericEvent(array('row' => $row, 'column' => $this->column, 'entity' => $this->entity, 'field' => $this->field))); }
/** * Configure all rows based upon a CSV. * * @param $csv * @throws \Exception */ public function setData($csv) { if (!$this->validate($csv)) { throw new \Exception('Not a valid CSV file!'); } $this->rows = array(); $rows = str_getcsv($csv, "\n"); foreach ($rows as $row) { $row = str_getcsv($row, $this->delimiter); $this->addRow($importRow = new ImportRow($this)); foreach ($row as $i => $value) { if (!array_key_exists($i, $this->mapping)) { throw new \Exception('The amount of mapped columns does not match the amount of columns in the CSV string.'); } $importRow->addDataColumn(new DataColumn($this->mapping[$i], $value)); } } }
/** * @inheritdoc */ public function run(ImportRow $row) { if (array_key_exists($this->column, $dataColumns = $row->getDataColumns())) { $dataColumns[$this->column]->setValue(call_user_func($this->closure, $dataColumns[$this->column])); } }