Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Annotation/ImportExclude.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Avro\CsvBundle\Annotation;

use Doctrine\ORM\Mapping\Annotation;

/**
* @Annotation
* @Target({"PROPERTY","ANNOTATION"})
*/
class ImportExclude
class ImportExclude implements Annotation
{
}
2 changes: 1 addition & 1 deletion Controller/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function processAction(Request $request, $alias)

$importer->init(sprintf('%s%s', $this->container->getParameter('avro_csv.tmp_upload_dir'), $form['filename']->getData()), $this->container->getParameter(sprintf('avro_csv.objects.%s.class', $alias), $form['delimiter']->getData()));

$importer->import($form['fields']->getData());
$importer->import($form['fields']->getData(), $form['dateFormat']->getData());

$this->container->get('session')->getFlashBag()->set('success', $importer->getImportCount().' items imported.');

Expand Down
199 changes: 0 additions & 199 deletions Doctrine/Importer.php

This file was deleted.

8 changes: 8 additions & 0 deletions Form/Type/ImportFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'choices' => $options['field_choices']
),
'allow_add' => true
))
->add('dateFormat', 'choice', array(
'label' => 'DateFormat',
'choices' => array(
'Y-m-d' => 'yyyy-mm-dd',
'm/d/Y' => 'mm/dd/yyyy',
'd/m/Y' => 'dd/mm/yyyy',
)
));

$builder->addEventListener(FormEvents::PRE_BIND, function (DataEvent $event) {
Expand Down
Empty file.
106 changes: 106 additions & 0 deletions Import/Doctrine/ORM/Importer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Avro\CsvBundle\Import\Doctrine\ORM;

use Avro\CsvBundle\Import\Importer as AbstractImporter;
use Avro\CsvBundle\Event\RowAddedEvent;

/**
* Import csv to doctrine entity
*
* @author Joris de Wit <joris.w.dewit@gmail.com>
*/
class Importer extends AbstractImporter
{
/**
* Add Csv row to db
*
* @param array $row An array of data
* @param array $fields An array of the fields to import
* @param string $dateFormat Date format
* @param boolean $andFlush Flush the ObjectManager
*/
protected function addRow($row, $fields, $dateFormat, $andFlush = true)
{
// Create new entity
$entity = new $this->class();

if (in_array('Id', $fields)) {
$key = array_search('Id', $fields);
if ($this->metadata->hasField('legacyId')) {
$entity->setLegacyId($row[$key]);
}
unset($fields[$key]);
}

// loop through fields and set to row value
foreach ($fields as $k => $v) {
$fieldName = lcfirst($v);
if ($this->metadata->hasField($fieldName)) {
$value = $row[$k];
switch ($this->metadata->getTypeOfField($fieldName)) {
case 'datetime':
$value = \DateTime::createFromFormat($dateFormat, $row[$k]);
break;
default:
$value = $row[$k];
break;
}
$entity->{'set'.$fields[$k]}($value);
} else if ($this->metadata->hasAssociation($fieldName)) {
$association = $this->metadata->associationMappings[$fieldName];
switch ($association['type']) {
case '1': // oneToOne
//Todo:
break;
case '2': // manyToOne
continue;
// still needs work
$joinColumnId = $association['joinColumns'][0]['name'];
$legacyId = $row[array_search($this->caseConverter->toCamelCase($joinColumnId), $this->headers)];
if ($legacyId) {
try {
$criteria = array('legacyId' => $legacyId);
if ($this->useOwner) {
$criteria['owner'] = $this->owner->getId();
}

$associationClass = new \ReflectionClass($association['targetEntity']);
if ($associationClass->hasProperty('legacyId')) {
$relation = $this->objectManager->getRepository($association['targetEntity'])->findOneBy($criteria);
if ($relation) {
$entity->{'set'.ucfirst($association['fieldName'])}($relation);
}
}
} catch(\Exception $e) {
// legacyId does not exist
// fail silently
}
}
break;
case '4': // oneToMany
//TODO:
break;
case '8': // manyToMany
//TODO:
break;
}
}
}

$this->dispatcher->dispatch('avro_csv.row_added', new RowAddedEvent($entity, $row, $fields));

$this->objectManager->persist($entity);

if ($andFlush) {
$this->objectManager->flush();
$this->objectManager->clear($this->class);
}
}

}
Loading