[TYPO3-mvc] Adding multiple files to a Model
Andy Pattynama
andy.pattynama at gmail.com
Wed Dec 18 12:10:57 CET 2013
I'm currently migrating an existing 4.5 installation to 6.1 and I need to import some old data. For that matter, I created a new Extbase extension with an ImportController and so far, I have no problems in importing normal text fields. But now, I'm stuck with handling importing images.
For creating the TCA, etc., I followed http://buzz.typo3.org/teams/extbase/article/fal-and-extbase-the-easy-part/
My code [1] is attached below. The images get imported into sys_file and are also selectable, when I try to add the images through the backend. The problem is, I can't add references to those files from my ImportController.
What I don't understand is: How can the property pictures have an \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
when all I have is a File? The FileReference should be created, when I attach the ObjectStorage to the property pictures, no? Basically, the problem I'm having is: How can I attach multiple files to a property.
Any help is appreciated.
1:
Model/Promotion.php
class Promotion extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
/**
* pictures
*
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
* @lazy
*/
protected $pictures;
**
* __construct
*
* @return Promotion
*/
public function __construct() {
//Do not remove the next line: It would break the functionality
$this->initStorageObjects();
$this->pictures = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}
/**
* Returns the pictures
*
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage
*/
public function getPictures() {
return $this->pictures;
}
/**
* Sets the pictures
*
* @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $pictures
* @return void
*
*/
public function setPictures($pictures) {
$this->pictures = $pictures;
}
TCA/Promotion.php:
$TCA['extension_model']['columns']['pictures']['config'] = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig('pictures');
Controller/ImportController.php:
protected function importPromotion($promotion) {
$this->promotionRepository = GeneralUtility::makeInstance('Vendor\\Extension\\Domain\\Repository\\PromotionRepository');
$pictures = $this->getPromotionPictures(explode(",", $promotion["bilder"]));
$promotionRecord->setPictures($pictures);
$this->promotionRepository->add($promotionRecord);
}
protected function getPromotionPictures($pictures) {
$storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
$storage = $storageRepository->findByUid(1);
$objectStorage = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage');
foreach($pictures as $picture) {
if(strlen($picture)>0) {
$oldFile = $_SERVER['DOCUMENT_ROOT'].'typo3temp/mypath/'.$picture;
$newFile = 'content/shared/'.$picture;
$newFolder = 'content/shared/';
if($storage->hasFile($newFile)) {
$fileObject = $storage->getFile($newFile);
} else {
$fileObject = $storage->addFile($oldFile, $storage->getFolder($newFolder), $picture);
}
$objectStorage->attach($fileObject);
}
}
return $objectStorage;
}
More information about the TYPO3-project-typo3v4mvc
mailing list