简体   繁体   中英

TYPO3 Remove data from the database

I have created an extension and i would like to import and remove data on my 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
  2. I have the static template included
  3. I have included the PID on the constant editor
  4. I cleared everything that has to do with cache
  5. I created 2 elements on the database and the pid=4 is there. That means that the constant editor setting works.

I am using 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).

 $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? It will not be nice if the ID reaches the 254206782.

Thanks in advance,

The uid field is AUTO_INCREMENT , which only gets reset when you drop+recreate the table.

So even after deleting the the row with the highest uid of 100 , the next new row will get 101 as uid .

You can do with some naked SQL. Also you can use some tools like phpMyAdmin, Adminer or Sequel if they support this actions in the GUI.

As TYPO3 does not delete any records but marks them deleted you first needs to realy remove the deleted records.

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.

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.

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).

/*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,

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