Beispiel #1
0
 public function testSimpleImport()
 {
     $lv = ActiveRecordModel::getNewInstance('Language');
     $lv->setID('xx');
     $lv->save();
     $profile = new CsvImportProfile('Product');
     $profile->setField(0, 'Product.sku');
     $profile->setField(1, 'Product.name', array('language' => 'en'));
     $profile->setField(2, 'Product.name', array('language' => 'xx'));
     $profile->setField(3, 'Product.shippingWeight');
     $profile->setParam('delimiter', ';');
     $csvFile = ClassLoader::getRealPath('cache.') . 'testDataImport.csv';
     file_put_contents($csvFile, 'test; "Test Product"; "Parbaudes Produkts"; 15' . "\n" . 'another; "Another Test"; "Vel Viens"; 12.44');
     $import = new ProductImport($this->getApplication());
     $csv = $profile->getCsvFile($csvFile);
     $cnt = $import->importFile($csv, $profile);
     $this->assertEquals($cnt, 2);
     $test = Product::getInstanceBySKU('test');
     $this->assertTrue($test instanceof Product);
     $this->assertEquals($test->shippingWeight->get(), '15');
     $this->assertEquals($test->getValueByLang('name', 'en'), 'Test Product');
     $another = Product::getInstanceBySKU('another');
     $this->assertEquals($another->getValueByLang('name', 'xx'), 'Vel Viens');
     unlink($csvFile);
 }
Beispiel #2
0
 public function importInstance($record, CsvImportProfile $profile)
 {
     if (array_key_exists('sku', $record)) {
         $instance = Product::getInstanceBySKU($record['sku']);
         $id = $instance ? $instance->getID() : 0;
         if ($this->allowOnly == self::CREATE && $id > 0) {
             throw new Exception('Record exists');
         }
         if ($this->allowOnly == self::UPDATE && $id == 0) {
             throw new Exception('Record not found');
         }
     } else {
         // if identified by smth else what then?
     }
     return parent::importInstance($record, $profile);
 }