[英]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.我正在使用 TYPO3 10,我想更改tt_content表中pi_flexform列中的特定文本/单词。
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.概念:我想使用QueryBuilder将表单文件夹从user_upload迁移到form_definitions (将项目从 TYPO3 8 更新到 10)。
I could just override the form_definitions folder back to the user_upload, but i want a clean structure.我可以将 form_definitions 文件夹覆盖回 user_upload,但我想要一个干净的结构。 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:
现在,如果我更改表单所在的文件夹,表单将不再工作,因为在数据库和pi_flexform列中存在以下内容:
...
<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/
.我想要的是将
1:/user_upload/
更改为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.我知道我必须将 UPDATE & SET 与 QueryBuilder 一起使用,但我不知道如何将它用于字符串中的特定字符串。
How do i achieve this?我如何实现这一目标?
In order to replace a specific text/word in a column, one can use QueryBuilder's UPDATE & SET functions.为了替换列中的特定文本/单词,可以使用 QueryBuilder 的 UPDATE & SET 函数。 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 .
QueryBuilder 仅对未设置为deleted=1或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.我们只想更新pi_flexform列中存在单词user_upload的条目。
->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.我们使用 SQL REPLACE命令来查找单词并替换它。 With the
$queryBuilder->createNamedParameter()
we avoid SQL injections.使用
$queryBuilder->createNamedParameter()
我们可以避免 SQL 注入。
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.请注意,这仅适用于特定的数据库类型,您需要自己注意安全性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.