【问题标题】:TCA property not workingTCA 属性不起作用
【发布时间】:2025-12-29 00:20:15
【问题描述】:

我的 TYPO3 中有一个名为 Location 的对象,用于存储有关城市的信息,包括 pdf 文件。 在后端,我可以毫无问题地上传 pdf,但是当我尝试显示它时,我得到 NULL。数据库中'pdf'的值不是NULL,但是当我调试<f:debug>{location}</f:debug>我得到pdf => NULL !!!

这是我的 TCA/位置:

$GLOBALS['TCA']['tx_locations_domain_model_location'] = array(
    'ctrl' => $GLOBALS['TCA']['tx_locations_domain_model_location']['ctrl'],

....
    'columns' => array(
...
        'pdf' => array(
            'exclude' => 1,
            'label' => 'LLL:EXT:locations/Resources/Private/Language/locallang_db.xlf:tx_locations_domain_model_location.pdf',
            'config' => array (
                'type' => 'group',
                'internal_type' => 'file',
                'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
                'max_size' => $GLOBALS['TYPO3_CONF_VARS']['BE']['maxFileSize'],
                'uploadfolder' => 'uploads/pics',
                'show_thumbs' => 1,
                'size' => 1,
                'minitems' => 0,
                'maxitems' => 1
            )
        ),
...

Getter 和 Setter 在 typo3conf/ext/locations/Classes/Domain/Model/Location.php 中应该没问题:

/**
 * Returns the pdf
 *
 * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $pdf
 */
public function getPdf() {
    return $this->pdf;
}

/**
 * Sets the pdf
 *
 * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $pdf
 * @return void
 */
public function setPdf(\TYPO3\CMS\Extbase\Domain\Model\FileReference $pdf) {
    $this->pdf = $pdf;
}

您可以从下面看到调试给出 pdf=> NULL:

我的 pdf 栏不为空:

我错过了什么吗??

更新: 在 Pavin 的回答之后,我可以在 PDF 中看到一些东西,但是我无法在 href 标签中获取文件的名称,也许我需要在新的答案之后更改一些东西。 我可以看到数据库中的内容发生了变化,pdf 字段现在是布尔值。这就是为什么我可能会得到 pdf0.originalResource NULL,但在后端我可以上传文件并在保存和刷新后查看!!!:

        <p><f:translate key="tx_locations_domain_model_location.downloadpdf" /> <a class="download" target="_blank" title="Initiates file download" href="{location.pdf.originalResource.publicUrl}"><f:translate key="tx_locations_domain_model_location.here" />..</a></p>

更新 2

我的新模型/L​​ocation.php

/**
     * pdf
     *
     * @var string
     */
    protected $pdf = NULL;

...
/**
     * Returns the pdf
     *
     * @return string $pdf
     */
    public function getPdf() {
        return $this->pdf;
    }

    /**
     * Sets the pdf
     *
     * @param string $pdf
     * @return void
     */
    public function setPdf($pdf) {
        $this->pdf = $pdf;
    }

我的 TCA/Location.php

'pdf' => array(
            'exclude' => 1,
            'label' => 'LLL:EXT:locations/Resources/Private/Language/locallang_db.xlf:tx_locations_domain_model_location.pdf',
            'config' => array (
                'type' => 'group',
                'internal_type' => 'file',
                'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
                'max_size' => $GLOBALS['TYPO3_CONF_VARS']['BE']['maxFileSize'],
                'uploadfolder' => 'uploads/pics',
                'show_thumbs' => 1,
                'size' => 1,
                'minitems' => 0,
                'maxitems' => 1
            )
        ),

【问题讨论】:

    标签: typo3 typo3-6.2.x


    【解决方案1】:

    您可以将以下 TCA 配置用于Doc 类型的记录。还在模型文件中添加 Getter 和 Setter 方法。

    'pdf' => array(
        'exclude' => 0,
        'label' => 'LLL:EXT:ext_key/Resources/Private/Language/locallang_db.xlf:label_key.files',
        'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
            'pdf',
            array('minitems' => 0,'maxitems' => 10),
            'pdf,doc,docx'
        ),
    ),
    

    对于 Model.php 文件。

        /**
         * pdf
         *
         *@var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
         */
        protected $pdf;
    

    initStorageObjects 方法:

        protected function initStorageObjects() {
            $this->files = new ObjectStorage();
            parent::initializeObject();
        }
    

    Getter 和 Setter 方法

       /**
         * Returns the pdf
         *
         * @return ObjectStorage
         */
        public function getPdf() {
            return $this->pdf;
        }
    
        /**
         * Sets the pdf
         *
         */
        public function setPdf($pdf) {
            $this->pdf = $pdf;
        }
    

    请参阅附件图片以获取 pdf 名称。请添加pdf标题。对于 Pdf 标题,您可以使用 {location.pdf.title}

    【讨论】:

    • 这不起作用,我什至看不到后端的 pdf 上传器
    • 我按照你说的更新了我的 Location.php,它没有用,现在我进入了后端“未知类型:”!!
    • initStorageObjects 方法中,例如 $this->pdf = new ObjectStorage();
    • 我没有 Model.php,也许你的意思是 Model.phpt 或 Model/Location.php ?无论如何,我都尝试了它们并没有工作,在前端我总是得到pdf => NULL
    • 添加此更改后,您需要安装/卸载扩展程序。你做到了吗?
    【解决方案2】:

    您使用group作为将文件保存在TCA中的类型,因此您的模型中的属性$pdf必须是string而不是\TYPO3\CMS\Extbase\Domain\Model\FileReference

    FileReference 仅适用于 FAL(文件抽象层)

    【讨论】:

    • 我在 Model/Lation.php 中将 $pdf 类型更改为字符串,在前端得到 {location.pdf} NULL,但在后端仍然可以
    • 更改属性后是否清除了系统缓存?你也更新了你的getter和setter吗?
    • 如果您已经将 TCA 更改为使用 FAL,则需要使用 FileReference。以下是有关您可以使用它做什么的详细答案:*.com/questions/40135241/…。文件名是{location.pdf.properties.name}
    • 仍然为 NULL,请参阅我的 UPDATE2
    • 你用的是什么typo3版本?