From 096904155e6eb97d9db5cd0edd88ecca18549529 Mon Sep 17 00:00:00 2001 From: Joris de Wit Date: Thu, 17 Jan 2013 18:28:04 -0800 Subject: [PATCH 1/3] Improve tests --- Annotation/ImportExclude.php | 5 +- Import/Doctrine/MongoDB/Importer.php | 0 .../Doctrine/ORM}/Importer.php | 114 +----------------- Import/Importer.php | 77 +----------- Import/ImporterInterface.php | 18 +++ Tests/Doctrine/ImporterTest.php | 66 ---------- Tests/Export/Doctrine/ORM/ExporterTest.php | 16 ++- Tests/Fixtures/ORM/TestEntity.php | 105 ++++++++++++++++ .../{ => Doctrine/ORM}/ImporterTest.php | 24 ++-- Tests/TestEntity.php | 40 ------ Tests/Util/FieldRetrieverTest.php | 21 ++-- Tests/Util/ReaderTest.php | 42 ++++--- Tests/import.csv | 8 +- composer.json | 2 + 14 files changed, 202 insertions(+), 336 deletions(-) create mode 100644 Import/Doctrine/MongoDB/Importer.php rename {Doctrine => Import/Doctrine/ORM}/Importer.php (50%) create mode 100644 Import/ImporterInterface.php delete mode 100644 Tests/Doctrine/ImporterTest.php create mode 100644 Tests/Fixtures/ORM/TestEntity.php rename Tests/Import/{ => Doctrine/ORM}/ImporterTest.php (71%) delete mode 100644 Tests/TestEntity.php diff --git a/Annotation/ImportExclude.php b/Annotation/ImportExclude.php index 0bd7c8c..f62d9f0 100644 --- a/Annotation/ImportExclude.php +++ b/Annotation/ImportExclude.php @@ -2,9 +2,12 @@ namespace Avro\CsvBundle\Annotation; +use Doctrine\ORM\Mapping\Annotation; + /** * @Annotation + * @Target({"PROPERTY","ANNOTATION"}) */ -class ImportExclude +class ImportExclude implements Annotation { } diff --git a/Import/Doctrine/MongoDB/Importer.php b/Import/Doctrine/MongoDB/Importer.php new file mode 100644 index 0000000..e69de29 diff --git a/Doctrine/Importer.php b/Import/Doctrine/ORM/Importer.php similarity index 50% rename from Doctrine/Importer.php rename to Import/Doctrine/ORM/Importer.php index 1bc7b9e..9fc4638 100644 --- a/Doctrine/Importer.php +++ b/Import/Doctrine/ORM/Importer.php @@ -5,113 +5,18 @@ * file that was distributed with this source code. */ -namespace Avro\CsvBundle\Doctrine; +namespace Avro\CsvBundle\Import\Doctrine\ORM; - -use Avro\CaseBundle\Util\CaseConverter; -use Avro\CsvBundle\Annotation\Exclude; +use Avro\CsvBundle\Import\Importer as AbstractImporter; use Avro\CsvBundle\Event\RowAddedEvent; -use Avro\CsvBundle\Util\Reader; - -use Doctrine\Common\Persistence\ObjectManager; - -use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** - * Import csv to doctrine entity/document + * Import csv to doctrine entity * * @author Joris de Wit */ -class Importer +class Importer extends AbstractImporter { - protected $fields; - protected $metadata; - protected $reader; - protected $batchSize; - protected $importCount = 0; - protected $caseConverter; - protected $objectManager; - - /** - * @param CsvReader $reader The csv reader - * @param Dispatcher $dispatcher The event dispatcher - * @param CaseConverter $caseConverter The case Converter - * @param ObjectManager $objectManager The Doctrine Object Manager - * @param int $batchSize The batch size before flushing & clearing the om - */ - public function __construct(Reader $reader, EventDispatcherInterface $dispatcher, CaseConverter $caseConverter, ObjectManager $objectManager, $batchSize) - { - $this->reader = $reader; - $this->dispatcher = $dispatcher; - $this->caseConverter = $caseConverter; - $this->objectManager = $objectManager; - $this->batchSize = $batchSize; - } - - /** - * Import a file - * - * @param File $file The csv file - * @param string $class The class name of the entity - * @param string $delimiter The csv's delimiter - * @param string $headerFormat The header case format - * - * @return boolean true if successful - */ - public function init($file, $class, $delimiter = ',', $headerFormat = 'title') - { - $this->reader->open($file, $delimiter); - $this->class = $class; - $this->metadata = $this->objectManager->getMetadataFactory()->getMetadataFor($class); - $this->headers = $this->caseConverter->convert($this->reader->getHeaders(), $headerFormat); - } - - /** - * Get the csv's header row - * - * @return array - */ - public function getHeaders() - { - return $this->headers; - } - - /** - * Get the csv's next row - * - * @return array - */ - public function getRow() - { - return $this->reader->getRow(); - } - - /** - * Import the csv and persist to database - * - * @param array $fields The fields to persist - * - * @return true if successful - */ - public function import($fields) - { - $fields = array_unique($this->caseConverter->toPascalCase($fields)); - - while ($row = $this->reader->getRow()) { - if (($this->importCount % $this->batchSize) == 0) { - $this->addRow($row, $fields, true); - } else { - $this->addRow($row, $fields, false); - } - $this->importCount++; - } - - // one last flush to make sure no persisted objects get left behind - $this->objectManager->flush(); - - return true; - } - /** * Add Csv row to db * @@ -119,7 +24,7 @@ public function import($fields) * @param array $fields An array of the fields to import * @param boolean $andFlush Flush the ObjectManager */ - private function addRow($row, $fields, $andFlush = true) + protected function addRow($row, $fields, $andFlush = true) { // Create new entity $entity = new $this->class(); @@ -187,13 +92,4 @@ private function addRow($row, $fields, $andFlush = true) } } - /** - * Get import count - * - * @return int - */ - public function getImportCount() - { - return $this->importCount; - } } diff --git a/Import/Importer.php b/Import/Importer.php index 138fb0a..77ce287 100644 --- a/Import/Importer.php +++ b/Import/Importer.php @@ -21,7 +21,7 @@ * * @author Joris de Wit */ -class Importer +abstract class Importer implements ImporterInterface { protected $fields; protected $metadata; @@ -91,81 +91,6 @@ public function import($fields) return true; } - /** - * Add Csv row to db - * - * @param array $row An array of data - * @param array $fields An array of the fields to import - * @param boolean $andFlush Flush the ObjectManager - */ - private function addRow($row, $fields, $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) { - if ($this->metadata->hasField(lcfirst($v))) { - $entity->{'set'.$fields[$k]}($row[$k]); - } else if ($this->metadata->hasAssociation(lcfirst($v))) { - $association = $this->metadata->associationMappings[lcfirst($v)]; - 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); - } - } - /** * Get import count * diff --git a/Import/ImporterInterface.php b/Import/ImporterInterface.php new file mode 100644 index 0000000..0d863cb --- /dev/null +++ b/Import/ImporterInterface.php @@ -0,0 +1,18 @@ + + */ +interface ImporterInterface +{ + +} diff --git a/Tests/Doctrine/ImporterTest.php b/Tests/Doctrine/ImporterTest.php deleted file mode 100644 index 9a31364..0000000 --- a/Tests/Doctrine/ImporterTest.php +++ /dev/null @@ -1,66 +0,0 @@ -getMock('Doctrine\Common\Persistence\Mapping\ClassMetadataFactory'); - $metadataFactory->expects($this->any()) - ->method('getMetadataFor') - ->will($this->returnValue(array( - 'fieldMappings' => array( - '0' => 'id' - ) - ))); - - $objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); - $objectManager->expects($this->any()) - ->method('getMetadataFactory') - ->will($this->returnValue($metadataFactory)); - $objectManager->expects($this->any()) - ->method('hasField') - ->will($this->returnValue(true)); - - $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - - $context = $this->getMock('Symfony\Component\Security\Core\SecurityContext'); - $class = 'Avro\CsvBundle\Tests\TestEntity'; - - $this->importer = new Importer($reader, $dispatcher, $caseConverter, $objectManager, 5); - $this->importer->init(__DIR__ . '/../import.csv', $class, ',', 'title'); - } - - /** - * Test getHeaders - */ - public function testGetHeaders() - { - $this->assertEquals( - array( - 0 => 'Header 1', - 1 => 'Header 2', - 2 => 'Header 3', - ), - $this->importer->getHeaders() - ); - } - - -} diff --git a/Tests/Export/Doctrine/ORM/ExporterTest.php b/Tests/Export/Doctrine/ORM/ExporterTest.php index aade139..1674251 100644 --- a/Tests/Export/Doctrine/ORM/ExporterTest.php +++ b/Tests/Export/Doctrine/ORM/ExporterTest.php @@ -18,7 +18,7 @@ class ExporterTest extends \PHPUnit_Framework_TestCase */ public function setUp() { - $query = $this->getMock('Doctrine\ORM\Query', array('iterate', 'HYDRATE_ARRAY'), array(), '', false); + $query = $this->getMock('Avro\CsvBundle\Tests\Export\Query', array('iterate'), array(), '', false); $query->expects($this->any()) ->method('iterate') ->will($this->returnValue(array(0 => array(0 => array('row 1' => 'val\'1', 'row 2' => 'val,2', 'row 3' => 'val"3'))))); @@ -30,9 +30,6 @@ public function setUp() $queryBuilder->expects($this->any()) ->method('from') ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->any()) - ->method('from') - ->will($this->returnValue($queryBuilder)); $queryBuilder->expects($this->any()) ->method('getQuery') ->will($this->returnValue($query)); @@ -44,6 +41,7 @@ public function setUp() ->will($this->returnValue($queryBuilder)); $this->exporter = new Exporter($entityManager); + $this->exporter->init('Avro\CsvBundle\Tests\Fixtures\ORM\TestEntity'); } /** @@ -51,7 +49,6 @@ public function setUp() */ public function testInit() { - $this->exporter->init('Avro\CsvBundle\Tests\TestEntity'); $this->assertTrue($this->exporter->getQueryBuilder() instanceof QueryBuilder); } @@ -76,7 +73,6 @@ public function testGetContent() $expected .= '"val\'1","val,2","val""3"'; $expected .= "\n"; - $this->exporter->init('Avro\CsvBundle\Tests\TestEntity'); $this->assertEquals( $expected, $this->exporter->getContent() @@ -84,3 +80,11 @@ public function testGetContent() } } + +class Query +{ + protected function iterate() + { + } + +} diff --git a/Tests/Fixtures/ORM/TestEntity.php b/Tests/Fixtures/ORM/TestEntity.php new file mode 100644 index 0000000..32aba27 --- /dev/null +++ b/Tests/Fixtures/ORM/TestEntity.php @@ -0,0 +1,105 @@ + + * + * @ORM\Entity + */ +class TestEntity +{ + /** + * @var integer + * + * @ORM\Column(name="id", type="integer") + * @ORM\Id + * @ORM\GeneratedValue(strategy="AUTO") + */ + protected $id; + + /** + * @var string + * + * @ORM\Column(type="string", length=100, nullable=true) + */ + protected $stringField; + + /** + * @var string + * + * @ORM\Column(type="string", length=100, nullable=true) + * @ImportExclude + */ + protected $stringField2; + + /** + * @var int + * + * @ORM\Column(type="integer", length=100, nullable=true) + */ + protected $integerField; + + /** + * @var \DateTime + * + * @ORM\Column(type="datetime", nullable=true) + */ + protected $dateField; + + public function getId() + { + return $this->id; + } + + public function setId($id) + { + $this->id = $id; + } + + public function getStringField() + { + return $this->stringField; + } + public function setStringField($stringField) + { + $this->stringField = $stringField; + } + + + public function getStringField2() + { + return $this->stringField2; + } + public function setStringField2($stringField2) + { + $this->stringField2 = $stringField2; + } + + public function getIntegerField() + { + return $this->integerField; + } + public function setIntegerField($integerField) + { + $this->integerField = $integerField; + } + + public function getDateField() + { + return $this->dateField; + } + + public function setDateField($dateField) + { + $this->dateField = $dateField; + return $this; + } +} + diff --git a/Tests/Import/ImporterTest.php b/Tests/Import/Doctrine/ORM/ImporterTest.php similarity index 71% rename from Tests/Import/ImporterTest.php rename to Tests/Import/Doctrine/ORM/ImporterTest.php index df2897b..9c66f41 100644 --- a/Tests/Import/ImporterTest.php +++ b/Tests/Import/Doctrine/ORM/ImporterTest.php @@ -1,9 +1,9 @@ fields = $fields; + $this->fields = $fields = array('id', 'stringField', 'integerField', 'dateField'); $caseConverter = new CaseConverter(); $reader = new Reader(); $metadata = $this->getMockForAbstractClass('Doctrine\Common\Persistence\Mapping\ClassMetadata', array('hasField')); - $metadata->expects($this->any()) + $metadata->expects($this->atLeastOnce()) ->method('hasField') ->will($this->returnCallback(function($value) use ($fields){ return in_array($value, $fields); })); $objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); - $objectManager->expects($this->any()) + $objectManager->expects($this->atLeastOnce()) ->method('getClassMetadata') ->will($this->returnValue($metadata)); $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $dispatcher->expects($this->any()) + $dispatcher->expects($this->atLeastOnce()) ->method('dispatch') ->will($this->returnValue('true')); $this->importer = new Importer($reader, $dispatcher, $caseConverter, $objectManager, 5); - $this->importer->init(__DIR__ . '/../import.csv', 'Avro\CsvBundle\Tests\TestEntity', ',', 'title'); + $this->importer->init(__DIR__ . '/../../../import.csv', 'Avro\CsvBundle\Tests\Fixtures\ORM\TestEntity'); } /** @@ -62,6 +60,14 @@ public function testImport() true, $this->importer->import($this->fields) ); + } + + /** + * Test number of row imported + */ + public function testImportCount() + { + $this->importer->import($this->fields); $this->assertEquals( 3, diff --git a/Tests/TestEntity.php b/Tests/TestEntity.php deleted file mode 100644 index 3517a16..0000000 --- a/Tests/TestEntity.php +++ /dev/null @@ -1,40 +0,0 @@ -id; - } - public function setId($id) - { - $this->id = $id; - } - - public function getField1() - { - return $this->field1; - } - public function setField1($field1) - { - $this->field1 = $field1; - } - - public function getField2() - { - return $this->field2; - } - public function setField2($field2) - { - $this->field2 = $field2; - } - -} diff --git a/Tests/Util/FieldRetrieverTest.php b/Tests/Util/FieldRetrieverTest.php index 1df853c..f0faaae 100644 --- a/Tests/Util/FieldRetrieverTest.php +++ b/Tests/Util/FieldRetrieverTest.php @@ -5,6 +5,7 @@ use Avro\CsvBundle\Util\FieldRetriever; use Avro\CaseBundle\Util\CaseConverter; +use Doctrine\Common\Annotations\AnnotationRegistry; use Doctrine\Common\Annotations\AnnotationReader; class FieldRetrieverTest extends \PHPUnit_Framework_TestCase @@ -14,10 +15,13 @@ class FieldRetrieverTest extends \PHPUnit_Framework_TestCase public function setUp() { + AnnotationRegistry::registerFile(__DIR__ . '/../../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); + AnnotationRegistry::registerFile(__DIR__ . '/../../Annotation/ImportExclude.php'); + $annotationReader = new AnnotationReader(); $caseConverter = new CaseConverter(); $this->fieldRetriever = new FieldRetriever($annotationReader, $caseConverter); - $this->class = 'Avro\CsvBundle\Tests\TestEntity'; + $this->class = 'Avro\CsvBundle\Tests\Fixtures\ORM\TestEntity'; } public function testGetFields() @@ -26,8 +30,9 @@ public function testGetFields() $this->fieldRetriever->getFields($this->class), array( '0' => 'Id', - '1' => 'Field1', - '2' => 'Field2', + '1' => 'String Field', + '2' => 'Integer Field', + '3' => 'Date Field', ) ); } @@ -38,8 +43,9 @@ public function testGetFieldsAsCamelCase() $this->fieldRetriever->getFields($this->class, 'camel'), array( '0' => 'id', - '1' => 'field1', - '2' => 'field2', + '1' => 'stringField', + '2' => 'integerField', + '3' => 'dateField', ) ); } @@ -50,8 +56,9 @@ public function testGetFieldsAndCopyKeys() $this->fieldRetriever->getFields($this->class, 'camel', true), array( 'id' => 'id', - 'field1' => 'field1', - 'field2' => 'field2', + 'stringField' => 'stringField', + 'integerField' => 'integerField', + 'dateField' => 'dateField', ) ); } diff --git a/Tests/Util/ReaderTest.php b/Tests/Util/ReaderTest.php index 1aeab3c..8d17826 100644 --- a/Tests/Util/ReaderTest.php +++ b/Tests/Util/ReaderTest.php @@ -18,9 +18,10 @@ public function testGetHeaders() { $this->assertEquals( array( - 0 => 'Header 1', - 1 => 'Header 2', - 2 => 'Header 3', + 0 => 'Id', + 1 => 'String Field', + 2 => 'Integer Field', + 3 => 'Date Field', ), $this->reader->getHeaders() ); @@ -30,25 +31,28 @@ public function testGetRow() { $this->assertEquals( array( - 0 => 'row1column1', - 1 => 'row1column2', - 2 => 'row1column3', + 0 => '1', + 1 => 'string 1', + 2 => '11', + 3 => '2012-01-01' ), $this->reader->getRow() ); $this->assertEquals( array( - 0 => 'row2column1', - 1 => 'row2column2', - 2 => 'row2column3', + 0 => '5', + 1 => 'string 2', + 2 => '22', + 3 => '2012-02-02' ), $this->reader->getRow() ); $this->assertEquals( array( - 0 => 'row3column1', - 1 => 'row3column2', - 2 => 'row3column3', + 0 => '10', + 1 => 'string 3', + 2 => '33', + 3 => '2012-03-03' ), $this->reader->getRow() ); @@ -59,14 +63,16 @@ public function testGetRows() $this->assertEquals( array( 0 => array( - 0 => 'row1column1', - 1 => 'row1column2', - 2 => 'row1column3', + 0 => '1', + 1 => 'string 1', + 2 => '11', + 3 => '2012-01-01' ), 1 => array( - 0 => 'row2column1', - 1 => 'row2column2', - 2 => 'row2column3', + 0 => '5', + 1 => 'string 2', + 2 => '22', + 3 => '2012-02-02' ), ), $this->reader->getRows(2) diff --git a/Tests/import.csv b/Tests/import.csv index b72812a..6a36fd6 100644 --- a/Tests/import.csv +++ b/Tests/import.csv @@ -1,4 +1,4 @@ -"Header 1", "Header 2", "Header 3" -"row1column1", "row1column2", "row1column3" -"row2column1", "row2column2", "row2column3" -"row3column1", "row3column2", "row3column3" +"Id", "String Field", "Integer Field", "Date Field" +"1", "string 1", "11", "2012-01-01" +"5", "string 2", "22", "2012-02-02" +"10", "string 3", "33", "2012-03-03" diff --git a/composer.json b/composer.json index 1dda5a3..7193966 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,8 @@ }, "require-dev": { "twig/twig": "*", + "doctrine/orm": ">=2.2.3,<2.4-dev", + "doctrine/mongodb-odm-bundle": "3.0.*", "doctrine/doctrine-bundle": "*" }, "autoload": { From c184ceecce0db47e1ac844f135c418538d1abc50 Mon Sep 17 00:00:00 2001 From: Joris de Wit Date: Thu, 17 Jan 2013 21:42:37 -0800 Subject: [PATCH 2/3] Added date format option --- Controller/ImportController.php | 2 +- Form/Type/ImportFormType.php | 8 +++++++ Import/Doctrine/ORM/Importer.php | 27 +++++++++++++++------- Import/Importer.php | 11 +++++---- Resources/views/Import/upload.html.twig | 1 + Tests/Import/Doctrine/ORM/ImporterTest.php | 4 ++-- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/Controller/ImportController.php b/Controller/ImportController.php index cd1df49..5e82fd4 100644 --- a/Controller/ImportController.php +++ b/Controller/ImportController.php @@ -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.'); diff --git a/Form/Type/ImportFormType.php b/Form/Type/ImportFormType.php index 70e6fae..179a410 100644 --- a/Form/Type/ImportFormType.php +++ b/Form/Type/ImportFormType.php @@ -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) { diff --git a/Import/Doctrine/ORM/Importer.php b/Import/Doctrine/ORM/Importer.php index 9fc4638..63845e4 100644 --- a/Import/Doctrine/ORM/Importer.php +++ b/Import/Doctrine/ORM/Importer.php @@ -20,11 +20,12 @@ 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 boolean $andFlush Flush the ObjectManager + * @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, $andFlush = true) + protected function addRow($row, $fields, $dateFormat, $andFlush = true) { // Create new entity $entity = new $this->class(); @@ -39,10 +40,20 @@ protected function addRow($row, $fields, $andFlush = true) // loop through fields and set to row value foreach ($fields as $k => $v) { - if ($this->metadata->hasField(lcfirst($v))) { - $entity->{'set'.$fields[$k]}($row[$k]); - } else if ($this->metadata->hasAssociation(lcfirst($v))) { - $association = $this->metadata->associationMappings[lcfirst($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: diff --git a/Import/Importer.php b/Import/Importer.php index 77ce287..34925b4 100644 --- a/Import/Importer.php +++ b/Import/Importer.php @@ -68,20 +68,23 @@ public function init($file, $class, $delimiter = ',', $headerFormat = 'title') /** * Import the csv and persist to database * - * @param array $fields The fields to persist + * @param array $fields The fields to persist + * @param string $dateFormat The date format for any datetime fields * * @return true if successful */ - public function import($fields) + public function import($fields, $dateFormat) { $fields = array_unique($this->caseConverter->toPascalCase($fields)); while ($row = $this->reader->getRow()) { if (($this->importCount % $this->batchSize) == 0) { - $this->addRow($row, $fields, true); + $flush = true; } else { - $this->addRow($row, $fields, false); + $flush = false; } + + $this->addRow($row, $fields, $dateFormat, $flush); $this->importCount++; } diff --git a/Resources/views/Import/upload.html.twig b/Resources/views/Import/upload.html.twig index f04e4ab..85fa8e6 100644 --- a/Resources/views/Import/upload.html.twig +++ b/Resources/views/Import/upload.html.twig @@ -7,6 +7,7 @@
{{ form_row(form.delimiter) }} + {{ form_row(form.dateFormat) }} {{ form_row(form.file) }} {{ form_row(form.filename) }} {{ form_widget(form._token) }} diff --git a/Tests/Import/Doctrine/ORM/ImporterTest.php b/Tests/Import/Doctrine/ORM/ImporterTest.php index 9c66f41..00ace99 100644 --- a/Tests/Import/Doctrine/ORM/ImporterTest.php +++ b/Tests/Import/Doctrine/ORM/ImporterTest.php @@ -58,7 +58,7 @@ public function testImport() { $this->assertEquals( true, - $this->importer->import($this->fields) + $this->importer->import($this->fields, 'Y-m-d') ); } @@ -67,7 +67,7 @@ public function testImport() */ public function testImportCount() { - $this->importer->import($this->fields); + $this->importer->import($this->fields, 'Y-m-d'); $this->assertEquals( 3, From 2bade954deb0ea45fcee86184fc829df42a11607 Mon Sep 17 00:00:00 2001 From: Joris de Wit Date: Thu, 17 Jan 2013 21:52:21 -0800 Subject: [PATCH 3/3] remove mongo --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 7193966..ee38230 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,6 @@ "require-dev": { "twig/twig": "*", "doctrine/orm": ">=2.2.3,<2.4-dev", - "doctrine/mongodb-odm-bundle": "3.0.*", "doctrine/doctrine-bundle": "*" }, "autoload": {