admin管理员组

文章数量:1125082

I'm trying to reproduce the example from the File upload | Typo3 Docs, but when I access the FileReference in the view or from PHP, it is always null. What am I missing?

The singleFile is null, and multipleFiles is empty. The backend displays the FileReference correctly in both cases without any issues. When I check the database, everything seems to be fine as well.

I'm using:

  • PHP version: 8.2.26
  • TYPO3 version: 13.4.2

Here is some code, but most of it is just copied from the example above:

Model

class Blog extends AbstractEntity
{
    // A single file
    protected ?FileReference $singleFile = null;

    /**
     * A collection of files.
     * @var ObjectStorage<FileReference>
     */
    protected ObjectStorage $multipleFiles;

    // When using ObjectStorages, it is vital to initialize these.
    public function __construct()
    {
        $this->multipleFiles = new ObjectStorage();
    }

    /**
     * Called again with initialize object, as fetching an entity from the DB does not use the constructor
     */
    public function initializeObject(): void
    {
        $this->multipleFiles = $this->multipleFiles ?? new ObjectStorage();
    }

    // Typical getters
    public function getSingleFile(): ?FileReference
    {
        return $this->singleFile;
    }

    /**
     * @return ObjectStorage|FileReference[]
     */
    public function getMultipleFiles(): ObjectStorage
    {
        return $this->multipleFiles;
    }

    // For later examples, the setters:
    public function setSingleFile(?FileReference $singleFile): void
    {
        $this->singleFile = $singleFile;
    }

    public function setMultipleFiles(ObjectStorage $files): void
    {
        $this->multipleFiles = $files;
    }
}

TCA

<?php

return [
    'ctrl' => [
      'title' => 'Blog',
      'label' => 'uid',
      'iconfile' => 'EXT:myext/Resources/Public/Icons/bx-book-alt.svg',
    ],
    'types' => [
      0 => [
        'showitem' => '
          singleFile,multipleFiles
        ',
      ],
    ],
    'columns' => [
        'singleFile' => [
            'exclude' => true,
            'label' => 'Single file',
            'config' => [
                'type' => 'file',
                'maxitems' => 1,
                'allowed' => 'common-image-types',
            ],
        ],
        'multipleFiles' => [
            'exclude' => true,
            'label' => 'Multiple files',
            'config' => [
                'type' => 'file',
                'allowed' => 'common-image-types',
            ],
        ],
    ],
];

ext_localconf.php

ExtensionUtility::configurePlugin(
    // extension name, matching the PHP namespaces (but without the vendor)
    'MyExt',
    // arbitrary, but unique plugin name (not visible in the backend)
    'Blog',
    // all actions
    [BlogController::class => 'show'],
    // non-cacheable actions
    [BlogController::class => 'show'],
    ExtensionUtility::PLUGIN_TYPE_CONTENT_ELEMENT,
);

Controller

class BlogController extends ActionController
{
    public function __construct(protected readonly BlogRepository $blogRepository)
    {
      // Note: The repository is a standard extbase repository, nothing specific
      //       to this example.
    }

    public function showAction(): ResponseInterface
    {
      $blog = $this->blogRepository->findAll()[0];
      $this->view->assign('blog', $blog);

      return $this->htmlResponse();
    }
}

I'm trying to reproduce the example from the File upload | Typo3 Docs, but when I access the FileReference in the view or from PHP, it is always null. What am I missing?

The singleFile is null, and multipleFiles is empty. The backend displays the FileReference correctly in both cases without any issues. When I check the database, everything seems to be fine as well.

I'm using:

  • PHP version: 8.2.26
  • TYPO3 version: 13.4.2

Here is some code, but most of it is just copied from the example above:

Model

class Blog extends AbstractEntity
{
    // A single file
    protected ?FileReference $singleFile = null;

    /**
     * A collection of files.
     * @var ObjectStorage<FileReference>
     */
    protected ObjectStorage $multipleFiles;

    // When using ObjectStorages, it is vital to initialize these.
    public function __construct()
    {
        $this->multipleFiles = new ObjectStorage();
    }

    /**
     * Called again with initialize object, as fetching an entity from the DB does not use the constructor
     */
    public function initializeObject(): void
    {
        $this->multipleFiles = $this->multipleFiles ?? new ObjectStorage();
    }

    // Typical getters
    public function getSingleFile(): ?FileReference
    {
        return $this->singleFile;
    }

    /**
     * @return ObjectStorage|FileReference[]
     */
    public function getMultipleFiles(): ObjectStorage
    {
        return $this->multipleFiles;
    }

    // For later examples, the setters:
    public function setSingleFile(?FileReference $singleFile): void
    {
        $this->singleFile = $singleFile;
    }

    public function setMultipleFiles(ObjectStorage $files): void
    {
        $this->multipleFiles = $files;
    }
}

TCA

<?php

return [
    'ctrl' => [
      'title' => 'Blog',
      'label' => 'uid',
      'iconfile' => 'EXT:myext/Resources/Public/Icons/bx-book-alt.svg',
    ],
    'types' => [
      0 => [
        'showitem' => '
          singleFile,multipleFiles
        ',
      ],
    ],
    'columns' => [
        'singleFile' => [
            'exclude' => true,
            'label' => 'Single file',
            'config' => [
                'type' => 'file',
                'maxitems' => 1,
                'allowed' => 'common-image-types',
            ],
        ],
        'multipleFiles' => [
            'exclude' => true,
            'label' => 'Multiple files',
            'config' => [
                'type' => 'file',
                'allowed' => 'common-image-types',
            ],
        ],
    ],
];

ext_localconf.php

ExtensionUtility::configurePlugin(
    // extension name, matching the PHP namespaces (but without the vendor)
    'MyExt',
    // arbitrary, but unique plugin name (not visible in the backend)
    'Blog',
    // all actions
    [BlogController::class => 'show'],
    // non-cacheable actions
    [BlogController::class => 'show'],
    ExtensionUtility::PLUGIN_TYPE_CONTENT_ELEMENT,
);

Controller

class BlogController extends ActionController
{
    public function __construct(protected readonly BlogRepository $blogRepository)
    {
      // Note: The repository is a standard extbase repository, nothing specific
      //       to this example.
    }

    public function showAction(): ResponseInterface
    {
      $blog = $this->blogRepository->findAll()[0];
      $this->view->assign('blog', $blog);

      return $this->htmlResponse();
    }
}
Share Improve this question asked 2 days ago DokuDoku 6182 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Columns in TCA and DB must be named as snake_case (single_file) instead of lowerCamelCase (singleFile).

'columns' => [
    'single_file' => [
        'exclude' => true,
        'label' => 'Single file',
        'config' => [
            'allowed' => 'common-image-types',
        ],
    ],
    'multiple_files' => [
        ...
    ],
]

本文标签: phpFileReference is always Null in extbase model objectStack Overflow