mario60

FLOW TYPO3 Collection

Aug 26th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.20 KB | None | 0 0
  1. use TYPO3\Flow\Annotations as Flow;
  2. use \TYPO3\Flow\Persistence\QueryInterface;
  3. use \TYPO3\Fluid\View\TemplateView;
  4. use \TYPO3\Flow\Property\TypeConverter\DateTimeConverter;
  5. use \Doctrine\Common\Collections\Collection;    <<<<< does it exist?
  6. use \Doctrine\Common\Collections\ArrayCollection;
  7.  
  8.  
  9.     (think of Crono as Blog, Evento as Post, Categoria as Category (of posts))
  10. $newFile: each file line contains data defining an Evento obj:          
  11.                         title#descrption#comment#dateBegin#dataEnd#catA:catB
  12.  
  13.     /**
  14.      * Adds the given new collection of Evento objects to the Evento repository
  15.      *
  16.          * @param \Mario\Crono\Domain\Model\Crono $crono
  17.          * @param array $newFile                          <<<< why do I need Array (I upload a single file)?
  18.      * @return void
  19.      */
  20.     public function createbulkAction(\Mario\Crono\Domain\Model\Crono $crono,  array $newFile) {
  21.  
  22. // thanks to http://www.layh.com/typo3-flow-tutorials/file-resources/comment-page-1/#comment-85622
  23.  
  24.         $fileResource = $this->resourceManager->importUploadedResource($newFile);
  25.         $uri = $fileResource->getUri();
  26.         // read file
  27.         $csvLines = array();
  28.         $fHandle = \fopen($uri, 'r');
  29.         if ($fHandle) {
  30.         while (($buffer = \fgets($fHandle, 4096)) !== false) {
  31.             $csvLines[] = $buffer;
  32.         }}
  33.        \fclose($fHandle);
  34.  
  35.     foreach($csvLines as $recordString){  
  36.         \TYPO3\FLOW\var_dump($recordString);
  37.                 $recordArr = explode("#", trim($recordString));
  38.              $evento = new \Mario\Crono\Domain\Model\Evento();
  39.              $evento->setCrono($crono);
  40.                      $evento->setTitolo($recordArr[0]);
  41.              $evento->setDescrizione($recordArr[1]);
  42.                      $evento->setCommento($recordArr[2]);
  43.  
  44.         $propertyMappingConfiguration = new \TYPO3\Flow\Property\PropertyMappingConfiguration();
  45.         $propertyMappingConfiguration->forProperty('datainizio')->setTypeConverterOption(
  46.                          'TYPO3\Flow\Property\TypeConverter\DateTimeConverter',
  47.                          \TYPO3\Flow\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
  48.                           'Y-m-d');
  49.  
  50.  
  51.                   $dataInizio = $this->propertyMapper->convert($recordArr[3], 'DateTime');
  52.                   $dataFine = $this->propertyMapper->convert($recordArr[4], 'DateTime');
  53.                   $evento->setDatainizio($dataInizio);
  54.                   $evento->setDatafine($dataFine);
  55. /// SET CATEGORIE .
  56.                  $categorias = new \Doctrine\Common\Collections\ArrayCollection;    
  57.          $evento->setCategorias($categorias);
  58. <<< I define a empty collection of categories and set to the object
  59.  
  60.          $catArr = explode(":", trim($recordArr[5]));
  61.                  
  62. foreach($catArr as $cat){$catObj = $this->categoriaRepository->findBySigla($cat)->getFirst();  $evento->addCategorias($catObj); }   //
  63.  
  64.                  $this->eventoRepository->add($evento);
  65.                   $crono->addEvento($evento);
  66.                   $this->cronoRepository->update($crono);
  67. }
  68.                
  69.                 $this->addFlashMessage('Created many new  Evento(s)');
  70.                 $this->redirect('index', 'Evento', NULL, array('crono' => $crono));
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment