xxx
xxx

Reputation: 111

TYPO3 database relationship for images in tt_content

I am currently developing an extension for TYPO3. For this purpose, I need to write a MySQL query to get the paths of uploaded images out of the database.

My question: the table tt_contents offers a field images. But this field only contains the amount of images referenced - not their UIDs. Image paths can be found in the table sys_file, but I can't see a way to connect these two tables.

How can I create a relationship between the elements of tt_contents and the embedded images from sys_file? How are these seemingly unconnected tables connected?

Upvotes: 0

Views: 2440

Answers (2)

vykk
vykk

Reputation: 11

Hope that helps understand the relation between tt_content, sys_file_reference and sys_file:

    select uid from sys_file_reference where tablenames = "tt_content" and uid_foreign = 185183 and fieldname = "image";
    #>> uid = 47079

    SELECT * FROM `sys_file_reference` where uid = 47079;
    #>> uid_local = 16573

    SELECT * FROM `sys_file` where uid = 16573;
    #>> identifier = /Ordner/Products/Pictures/Bild.jpg



    select uid from sys_file_reference where tablenames = "tt_content" and uid_foreign = 185184 and fieldname = "image";
    #>> uid = 46868

    SELECT * FROM `sys_file_reference` where uid = 46868;
    #>> uid_local = 48

    SELECT * FROM `sys_file` where uid = 48;
    #>> identifier = /Ordner/AnderesBild.png



    UPDATE sys_file_reference SET uid_local = 16573 WHERE uid_local = 48 and uid = 46868;
    #>> im Content Element (CType = image) angegebenes Bild ist nun auf ein anderes umgestellt

Upvotes: 1

Merec
Merec

Reputation: 2761

Here is a small snippet I use to get uploaded images from a content object (tt_content).

public function getContentImages($tt_content_uid) {
    /** @var \TYPO3\CMS\Core\Resource\FileRepository $fileRepository */
    $fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\FileRepository');
    $fileObjects = $fileRepository->findByRelation('tt_content', 'image', $tt_content_uid);

    // Get file information
    $files = array();
    foreach ($fileObjects as $key => $value) {
        $file = array();
        $file['reference'] = $value->getReferenceProperties();
        $file['original'] = $value->getOriginalFile()->getProperties();
        $files[] = $file;
    }

    return $files;
}

Upvotes: 3

Related Questions