Ausgangssituation: in einem sehr alten Typo3 existierten die Bilder der tx_members im Folder uploads/tx_members/ und waren in der Datenbank nur als Dateiname (logo_alt) gespeichert. Um daraus dann FileReference Objekte zu erzeugen, habe ich folgendes gemacht.
1. FileReference angelegt
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder {
/**
* Uid of the referenced sys_file. Needed for extbase to serialize the
* reference correctly.
*
* @var int
*/
protected $uidLocal;
/**
* @var string
*/
protected $tablenames = 'tx_vendorext_domain_model_member';
/**
* @param \TYPO3\CMS\Core\Resource\ResourceInterface $originalResource
*/
public function setOriginalResource(\TYPO3\CMS\Core\Resource\ResourceInterface $originalResource) {
$this->originalResource = $originalResource;
$this->uidLocal = (int)$originalResource->getUid();
}
/**
* @return \TYPO3\CMS\Core\Resource\FileReference
*/
public function getOriginalResource() {
if($this->originalResource === NULL) {
$this->originalResource = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileReferenceObject(
$this->getUid()
);
}
return $this->originalResource;
}
/**
* @return string
*/
public function getTablenames() {
return $this->tablenames;
}
/**
* @param string $tablenames
*/
public function setTablenames($tablenames) {
$this->tablenames = $tablenames;
}
/**
* setFile
*
* @param \TYPO3\CMS\Core\Resource\File $falFile
* @return void
*/
public function setFile(\TYPO3\CMS\Core\Resource\File $falFile) {
$this->originalFileIdentifier = (int)$falFile->getUid();
}
}
2. mapping in ext_typoscript_setup.txt festgelegt:
config.tx_extbase {
persistence{
classes {
Vendor\Ext\Domain\Model\FileReference {
mapping {
tableName = sys_file_reference
columns {
uid_local.mapOnProperty = originalFileIdentifier
}
}
}
}
}
}
3. im Controller einmal alles durchlaufen und neu in „logo“ gespeichert:
private function setLogos(){
$storageRepository = $this->objectManager->get('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
$storage = $storageRepository->findByUid('1');
$targetFolder = $storage->getFolder('tx_members/');
$persistenceManager = $this->objectManager->get('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
$members = $this->memberRepository->findAll();
foreach($members as $member){
$logoAlt = $member->getLogoAlt();
if(!$member->getLogo() && $logoAlt && file_exists(PATH_site.'uploads/tx_members/'.$logoAlt)) {
$file = $storage->addFile('uploads/tx_members/' . $logoAlt, $targetFolder, $logoAlt, 'replace', false);
$newFileReference = $this->objectManager->get('Vendor\\Ext\\Domain\\Model\\FileReference');
$newFileReference->setFile($file);
$member->setLogo($newFileReference);
$this->memberRepository->update($member);
$persistenceManager->persistAll();
}
}
}