简体   繁体   English

为什么truncateTable在Joomla 3.7中不起作用?

[英]Why won't truncateTable work in Joomla 3.7?

I have the following code attempting to truncate a table. 我有以下代码尝试截断表。 The Joomla documentation makes me believe this will work, but it does not. Joomla文档使我相信这将起作用,但事实并非如此。 What am I missing? 我想念什么?

$db = JFactory::getDbo();
truncate_query = $db->getQuery(true);
//$truncate_query = 'TRUNCATE ' . $db->quoteName('#__mytable');
$truncate_query->truncateTable($db->quoteName('#__mytable'));
$db->setQuery($truncate_query);
echo $truncate_query;
exit();

If I use the line that is commented out to manually generate the SQL, it does work. 如果我使用注释掉的行来手动生成SQL,它确实可以工作。 The reason I am still looking to use the truncateTable function is that I am trying to include the truncation in a transaction. 我仍然希望使用truncateTable函数的原因是我试图在事务中包括截断。 When I use the manual statement, the table is still truncated even if another part of the transaction fails, which is annoying since the other statements rely on data that is truncated, so if the table is emptied when it shouldn't be there is no data left to run the transaction again. 当我使用手动语句时,即使事务的另一部分失败,该表仍会被截断,这很烦人,因为其他语句依赖于被截断的数据,因此,如果在不应有该表的情况下清空了该表,则不会剩下的数据可以再次运行事务。 Very annoying! 很烦人!

Here's how you call/execute your truncation query: 这是调用/执行截断查询的方法:

JFactory::getDbo()->truncateTable('#__mytable');

And now some more details... 现在还有更多细节...

Here is the method's code block in the Joomla source code: 这是Joomla源代码中方法的代码块:

 public function truncateTable($table) { $this->setQuery('TRUNCATE TABLE ' . $this->quoteName($table)); $this->execute(); } 

As you can see the truncateTable() method expects a tablename as a string for its sole parameter; 如您所见, truncateTable()方法期望将表名作为唯一参数的字符串。 you are offering a backtick-wrapped string -- but the method already offers the backtick-wrapping service. 您正在提供反引号包装的字符串-但该方法已经提供了反引号包装的服务。 (Even if you strip your backticks off, your approach will not be successful.) (即使删除了反引号,您的方法也不会成功。)

The setQuery() and execute() calls are already inside the method, so you don't need to create a new query object nor execute anything manually. setQuery()execute()调用已经在方法内部,因此您无需创建新的查询对象或手动执行任何操作。

There is no return in the method, so the default null is returned -- ergo, your $truncate_query becomes null . 方法中没有返回值,因此将返回默认的null ,因此, $truncate_query变为null When you try to execute(null) , you get nothing -- not even an error message. 当您尝试execute(null) ,您什么也没得到-甚至没有错误消息。

If you want to know how many rows were removed, you will need to run a SELECT query before hand to count the rows. 如果您想知道删除了多少行,则需要先运行SELECT查询才能对行进行计数。

If you want to be sure that there are no remaining rows of data, you'll need to call a SELECT and check for zero rows of data. 如果要确保没有剩余的数据行,则需要调用SELECT并检查数据的零行。

Here is my answer (with different wording) on your JSX question. 这是对JSX问题的回答 (用不同的措词)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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