简体   繁体   中英

TYPO3: How do i change a part of a string in the database with QueryBuilder?

I am using TYPO3 10 and i would like to change a specific text/word inside the pi_flexform column in the tt_content table.

The concept: I want to migrate the forms folder from user_upload to form_definitions (Updating a Project from TYPO3 8 to 10) using the QueryBuilder.

I could just override the form_definitions folder back to the user_upload, but i want a clean structure. Now if i change the folder from where the forms are coming, the forms do not work anymore because in the database and in the pi_flexform column exists the following:

...
<field index="settings.persistenceIdentifier">
      <value index="vDEF">1:/user_upload/NameOfTheForm.form.yaml</value>
</field>
...

What i want, is to change the 1:/user_upload/ to 1:/form_definitions/ .

I know i will have to use the UPDATE & SET with QueryBuilder but i do not know how to use it for a specific string inside a string.

How do i achieve this?

In order to replace a specific text/word in a column, one can use QueryBuilder's UPDATE & SET functions. It looks like this:

$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder
    ->update('tt_content')
    ->where(
        $queryBuilder->expr()->andX(
            $queryBuilder->expr()->like(
                'tt_content.pi_flexform',
                   $queryBuilder->createNamedParameter(
                      '%user_upload%',
                      Connection::PARAM_STR
                   )
             )
         )
     )
     ->set(
         'tt_content.pi_flexform',
         sprintf(
             'REPLACE(tt_content.pi_flexform, %s, %s)',
              $queryBuilder->createNamedParameter('user_upload', Connection::PARAM_STR),
              $queryBuilder->createNamedParameter('form_definitions', Connection::PARAM_STR)
         ),
         false
     )
     ->execute();

Breaking the code down:

$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll();

We initialise the query and we are removing all the restrictions. QueryBuilder only performs to the entries that are not set to deleted=1 or hidden =1 . We want to perform the updated path to all entries, so we remove the restrictions.

$queryBuilder->expr()->like(
   'tt_content.pi_flexform',
       $queryBuilder->createNamedParameter(
       '%user_upload%',
        Connection::PARAM_STR
    )
 )

We only want to update the entries that in the pi_flexform column the word user_upload exists.

 ->set(
     'tt_content.pi_flexform',
     sprintf(
         'REPLACE(tt_content.pi_flexform, %s, %s)',
          $queryBuilder->createNamedParameter('user_upload', Connection::PARAM_STR),
          $queryBuilder->createNamedParameter('form_definitions', Connection::PARAM_STR)
     ),
     false
 )
 ->execute();

We use the SQL REPLACE command to find the word and replace it. With the $queryBuilder->createNamedParameter() we avoid SQL injections.


Hope it helped

Best regards

You can also use a raw DB query with

$connection->query('Your raw query');

Be aware that this will only work for the specific database type and you need to take care about security yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM