Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typo3 使用2个表和QueryBuilder执行简单的UPDATESQL语句_Typo3 - Fatal编程技术网

Typo3 使用2个表和QueryBuilder执行简单的UPDATESQL语句

Typo3 使用2个表和QueryBuilder执行简单的UPDATESQL语句,typo3,Typo3,我正在为TYPO3 8.x及更高版本编程一个扩展。我不想使用不推荐使用的$GLOBALS['TYPO3_DB']->exec_*函数 我有一个简单的SQL语句,应该在class.ext_update.php类中使用,以便迁移数据: update table1 as t1,table2 as t2 set t1.field1=t2.field2 where t1.uid=t2.uid; 如您所见,没有用户输入,不需要引用任何内容。因此,我最好寻找一种使用原始SQL查询的方法 QueryBuild

我正在为TYPO3 8.x及更高版本编程一个扩展。我不想使用不推荐使用的$GLOBALS['TYPO3_DB']->exec_*函数

我有一个简单的SQL语句,应该在class.ext_update.php类中使用,以便迁移数据:

update table1 as t1,table2 as t2 set t1.field1=t2.field2 where t1.uid=t2.uid;
如您所见,没有用户输入,不需要引用任何内容。因此,我最好寻找一种使用原始SQL查询的方法

QueryBuilder始终为特定表显式设置。您可以做的是加入,但我没有让它与更新一起工作

$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
    ->getQueryBuilderForTable('table1');

$queryBuilder->update('table1')
   ->join(
       'table1', 
       'table2', 
       't2',
       $queryBuilder->expr()->eq(
           't2.uid', 
           $queryBuilder->quoteIdentifier('table1.uid')
       )
   )
   ->set(
       'table1.field1', 
       $queryBuilder->quoteIdentifier(
           't2.field2'
       ), 
       false
   )->execute();
“执行“更新
table1
SET
table1
field1
=
t2
field2
”:字段列表中的未知列“t2.field2”

更新:

$queryBuilder->getSQL()返回:


好的,所以没有应用连接。无论如何,我在文档中只找到了SELECT+JOIN的例子,这显然不是应该采用的方式

官方Type3文件声明:

对join()方法的调用只考虑->select()和 ->count()类型查询。->delete()、->insert()和update()不支持联接,这些查询部分将被忽略,并且不会在 最后声明

这是法律规定的限制

您可以使用条令连接对象的executeQuery或executeUpdate来执行原始查询。这不是一个经过测试的示例,但类似的东西应该可以工作:

$updateQuery = "UPDATE table1
                SET $connection->quoteIdentifier('table1.field1') = $connection->quoteIdentifier('table2.field2')
                WHERE $connection->quoteIdentifier('table1.uid') = $connection->quoteIdentifier('table2.uid')"; 
$connection->executeQuery($updateQuery);

官方的Type3文件说明:

对join()方法的调用只考虑->select()和 ->count()类型查询。->delete()、->insert()和update()不支持联接,这些查询部分将被忽略,并且不会在 最后声明

这是法律规定的限制

您可以使用条令连接对象的executeQuery或executeUpdate来执行原始查询。这不是一个经过测试的示例,但类似的东西应该可以工作:

$updateQuery = "UPDATE table1
                SET $connection->quoteIdentifier('table1.field1') = $connection->quoteIdentifier('table2.field2')
                WHERE $connection->quoteIdentifier('table1.uid') = $connection->quoteIdentifier('table2.uid')"; 
$connection->executeQuery($updateQuery);

你能重新格式化你的代码吗?很难看出什么在哪里开始和结束。在执行之前,您是否使用
$queryBuilder->getSQL()
检查结果?@Mathias谢谢您的提示,我更新了我的问题。您能重新格式化代码吗?很难看出什么在哪里开始和结束。您是否在执行之前使用
$queryBuilder->getSQL()
检查结果?@Mathias谢谢您的提示,我更新了我的问题。使用您的查询,稍微修改:“更新表1,表2集合”$连接->quoteIdentifier('table1.field1')。='$连接->引用标识符('table2.field2')。'在哪里$连接->quoteIdentifier('table1.uid')。='$连接->引用标识符('table2.uid');使用您的查询,稍微修改:“更新表1,表2集合”$连接->quoteIdentifier('table1.field1')。='$连接->引用标识符('table2.field2')。'在哪里$连接->quoteIdentifier('table1.uid')。='$连接->引用标识符('table2.uid');