简体   繁体   English

TYPO3从数据库中删除数据

[英]TYPO3 Remove data from the database

I have created an extension and i would like to import and remove data on my ImportAction. 我创建了一个扩展,我想导入和删除ImportAction上的数据。 Although the Import snippet works, the remove does not. 尽管导入代码段有效,但删除无效。

Note that this did not work for me. 请注意, 对我不起作用。

Here is what i have so far: 这是我到目前为止所拥有的:

  1. I installed my extension on my TYPO3 installation 我在TYPO3安装上安装了扩展程序
  2. I have the static template included 我包含了静态模板
  3. I have included the PID on the constant editor 我已将PID包含在常数编辑器中
  4. I cleared everything that has to do with cache 我清除了所有与缓存有关的内容
  5. I created 2 elements on the database and the pid=4 is there. 我在数据库上创建了2个元素,并且pid = 4在那里。 That means that the constant editor setting works. 这意味着常量编辑器设置有效。

I am using TYPO3 7.6.23 我正在使用TYPO3 7.6.23

Here is my code that does not work: 这是我的代码不起作用:

$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$myRepository = $objectManager->get('BW....\MyImporter\Domain\Repository\MyRepository');
$myRepository->removeAll();

Here is the code that imports data successfully (Here i am using USE on the top of my PHP file that is the reason why there is this myImport::class). 这是成功导入数据的代码(这是我在PHP文件顶部使用USE的原因,这就是为什么存在此myImport :: class的原因)。

 $finalTitle = 'This is a Test';
 $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
 $newMyImport = $objectManager->get(myImport::class);
 $newMyImport->setTitle($finalTitle);
 $this->myRepository->add($newMyImport);
 print_r($newMyImport);

How can i remove all the elements from my database ALONG WITH the IDs? 我如何从ID中删除数据库中的所有元素? It will not be nice if the ID reaches the 254206782. 如果ID到达254206782,那就不好了。

Thanks in advance, 提前致谢,

The uid field is AUTO_INCREMENT , which only gets reset when you drop+recreate the table. uid字段为AUTO_INCREMENT ,仅在您拖放并重新创建表时才会重置。

So even after deleting the the row with the highest uid of 100 , the next new row will get 101 as uid . 因此,即使删除uid最高的行100 ,下一个新行的uid也将变为101

You can do with some naked SQL. 您可以使用一些裸露的SQL。 Also you can use some tools like phpMyAdmin, Adminer or Sequel if they support this actions in the GUI. 如果您在GUI中支持此操作,也可以使用phpMyAdmin,Adminer或Sequel等工具。

As TYPO3 does not delete any records but marks them deleted you first needs to realy remove the deleted records. 由于TYPO3不会删除任何记录,而是将它们标记为已删除,因此您首先需要真正删除已删除的记录。

DELETE * FROM <table> WHERE deleted = 1;

Then you need to evaluate a new autoincrement value, which could be: 然后,您需要评估一个新的自动增量值,该值可以是:

SELECT max(uid) from <table>;

Add 1 to the result. 将1加到结果中。

As this still could be a very high value (eg if the last record was not deleted ) you might use a lower value and the next free id will be used. 由于这仍可能是一个很高的值(例如,如果未删除最后一条记录),则可以使用一个较低的值,并使用下一个空闲ID。

And now you could set the autoincrement to your value: 现在,您可以将自动增量设置为您的值:

ALTER TABLE <table> AUTO_INCREMENT=<your value>; 

A little bit late but i figured a way to do it and it works perfectly. 有点晚了,但我想出了一种方法,它可以完美地工作。

I had to truncate my table! 我不得不截断桌子!

This is the code on my Task.php (because i had it on my scheduler). 这是我Task.php上的代码(因为我的调度程序上有它)。

/*Create connection to Truncate the database*/
    $servername = TYPO3_db_host;
    $username = TYPO3_db_username;
    $password = TYPO3_db_password;
    $dbname = TYPO3_db;
    // Create connection
    $databaseConnect = mysqli_connect($servername, $username, $password, $dbname);
    $repositoryRemove = "TRUNCATE TABLE tx_importer_domain_model_import";
    $repositoryAttachmentsRemove = "TRUNCATE TABLE tx_importer_domain_model_attachments";
    $mysql = mysqli_query($databaseConnect, $repositoryRemove);
    $mysql1 = mysqli_query($databaseConnect, $repositoryAttachmentsRemove);
    mysqli_close($databaseConnect);

And simply as that, the deletion of my Tables was a success! 如此简单,删除我的表就成功了!

Best regards, 最好的祝福,

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

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