1

我正在开发一个扩展,它显示sys_file_reference中引用的所有文件。还可以根据sys_file_metadata中的新自定义属性对每个文件进行分类和过滤。

获取和过滤所有引用文件作为TYPO3\CMS\Core\Resource\File模型的最佳方法是什么?

在我的帮助下,TYPO3\CMS\Core\Resource\FileRepository我能够获得File包括元数据的模型。但是存储库总是期望 auid检索一个File. 现在我用我的存储库获取所有引用的文件并将它们传递给findFileReferenceByUid()方法。

class DocumentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {

    /**
     * Disables pid constraint
     *
     * @return void
     */
    public function initializeObject() {
        $querySettings = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
        $querySettings->setRespectStoragePage(FALSE);
        $this->setDefaultQuerySettings($querySettings);
    }

    /**
     * Finds all referenced documents returning them as File modules
     *
     * @return void
     */
    public function findAllReferenced() {
        $fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
        $query = $this->createQuery();
        $documents = $query->execute();
        $references = array();

        foreach ($documents as $document) {
            $references[] = $fileRepository->findFileReferenceByUid($document->getUid());
        }

        return $references;
    }
}

有没有更简单的方法,我该如何实现过滤器?

更新

现在我绕过 extbase 并直接使用数据库连接。TYPO3 在内部经常这样做。

/**
 * Finds all referenced documents returning them as File models
 *
 * @return array
 */
public function findAllReferenced() {

    $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
        'sys_file AS sf
            LEFT JOIN tt_content AS ttc ON ttc.header_link=CONCAT("file:", sf.uid)
            LEFT JOIN sys_file_reference AS ref ON sf.uid=ref.uid_local
            LEFT JOIN sys_file_metadata AS meta ON sf.uid=meta.file',
        '((ttc.uid IS NOT NULL AND ttc.hidden = 0 AND ttc.deleted = 0) OR (ref.uid IS NOT NULL AND ref.hidden = 0 AND ref.deleted = 0))',
        'sf.uid'
    );

    $documents = array();

    if (!empty($rows)) {
        foreach ($rows as $row) {
            $documents[] = $this->createDomainObject($row);
        }
    }
    return $documents;
}
4

0 回答 0