Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefglatz/69da48be06166702a8b1 to your computer and use it in GitHub Desktop.
Save josefglatz/69da48be06166702a8b1 to your computer and use it in GitHub Desktop.
ext:news v1.3.1 to v2.3.1: Update related files to mm relations (if you are using MM relation)
<?php
protected function updateFileRelations() {
$title = "Update related files";
$countNewsWithFileRelation = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'tx_news_domain_model_news', 'deleted=0 AND related_files != ""');
$countFilesWithParent = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'tx_news_domain_model_file', 'deleted=0 AND parent != 0');
if ($countFilesWithParent === 0 && $countNewsWithFileRelation > 0) {
$newsCount = 0;
// select news with related files
$newsQuery = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,related_files', 'tx_news_domain_model_news', 'deleted=0 AND related_files !=""');
while ($newsRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($newsQuery)) {
$newsCount++;
$news = $newsRow['uid'];
$relatedFilesUids = explode(',', $newsRow['related_files']);
// update news
$update = array('related_files' => count($relatedFilesUids));
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_news_domain_model_news', 'uid=' . $news, $update);
foreach ($relatedFilesUids as $relatedFile) {
// update file
$update = array('parent' => $news);
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_news_domain_model_file', 'uid=' . $relatedFile, $update);
}
}
$GLOBALS['TYPO3_DB']->sql_free_result($newsQuery);
$this->messageArray[] = array(t3lib_FlashMessage::OK, $title, $newsCount . ' news records have been updated!');
} else {
if ($countNewsWithFileRelation == 0) {
$this->messageArray[] = array(t3lib_FlashMessage::NOTICE, $title, 'Nothing to do! There are no news with related files');
}
if ($countFilesWithParent != 0) {
$this->messageArray[] = array(t3lib_FlashMessage::NOTICE, $title, 'Can not update because there are files with new relation model!');
}
}
}
<?php
/**
* news records got a relation to files and the relation uses now a mm query
* This method allows to update the mm table to got everything in sync again
* (after updating ext:news from v1.3.1 to v2.3.1)
*
* @author Josef Florian Glatz <j.glatz@opendo.at>, opendo GmbH
*
* @return void
*/
protected function updateRelatedFilesToMm() {
$title = 'Update files relation';
$countMmTable = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*'. 'tx_news_domain_model_news_file_mm', '1=1');
$countFilesWithParent = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'tx_news_domain_model_file', 'deleted=0 AND parent !=0');
$countNewsWithFileRelation = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'tx_news_domain_model_news', 'deleted=0 AND related_files != ""');
if (!$countMmTable && $countNewsWithFileRelation > 0) {
$newsCount = 0;
// select all news items with related_files
$newsQuery = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,related_files', 'tx_news_domain_model_news', 'deleted=0 AND related_files != ""');
while ($newsRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($newsQuery)) {
$newsCount++;
$news = $newsRow['uid'];
$relatedFileUids = explode(',', $newsRow['related_files']);
$i = 1;
foreach ($relatedFileUids as $relatedFile) {
// Insert mm relation
$insert = array(
// uid of news item
'uid_local' => $news,
// uid of related file
'uid_foreign' => $relatedFile,
'sorting' => $i++
);
$GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_news_domain_model_news_file_mm', $insert);
// Update related file
$update = array(
'parent' => $news
);
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_news_domain_model_file', 'uid=' . $relatedFile, $update);
}
// Update news record
$update = array('related_files' => count($relatedFileUids));
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_news_domain_model_news', 'uid=' . $news, $update);
}
$GLOBALS['TYPO3_DB']->sql_free_result($newsQuery);
$this->messageArray[] = array(t3lib_FlashMessage::OK, $title, $newsCount . ' news records have been updated!');
} else {
if ($countNewsWithFileRelation == 0) {
$this->messageArray[] = array(t3lib_FlashMessage::NOTICE, $title, 'Nothing to do! There are no news with related files');
}
if ($countFilesWithParent != 0) {
$this->messageArray[] = array(t3lib_FlashMessage::NOTICE, $title, 'Can not update because there are files with new relation model!');
}
}
}
diff --git a/class.ext_update.php b/class.ext_update.php
index ee5b310..5318bc5 100644
--- a/class.ext_update.php
+++ b/class.ext_update.php
@@ -64,6 +64,7 @@ class ext_update {
protected function processUpdates() {
$this->updateContentRelationToMm();
+ $this->updateRelatedFilesToMm();
/*
$this->renameDatabaseTable('tx_news2_domain_model_news', 'tx_news_domain_model_news');
@@ -136,6 +137,65 @@ class ext_update {
}
/**
+ * news records got a relation to files and the relation uses now a mm query
+ * This method allows to update the mm table to got everything in sync again
+ * (after updating ext:news from v1.3.1 to v2.3.1)
+ *
+ * @author Josef Florian Glatz <j.glatz@opendo.at>, opendo GmbH
+ *
+ * @return void
+ */
+ protected function updateRelatedFilesToMm() {
+ $title = 'Update files relation';
+
+ $countMmTable = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*'. 'tx_news_domain_model_news_file_mm', '1=1');
+ $countFilesWithParent = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'tx_news_domain_model_file', 'deleted=0 AND parent !=0');
+ $countNewsWithFileRelation = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'tx_news_domain_model_news', 'deleted=0 AND related_files != ""');
+
+ if (!$countMmTable && $countNewsWithFileRelation > 0) {
+ $newsCount = 0;
+ // select all news items with related_files
+ $newsQuery = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,related_files', 'tx_news_domain_model_news', 'deleted=0 AND related_files != ""');
+ while ($newsRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($newsQuery)) {
+ $newsCount++;
+ $news = $newsRow['uid'];
+ $relatedFileUids = explode(',', $newsRow['related_files']);
+ $i = 1;
+ foreach ($relatedFileUids as $relatedFile) {
+ // Insert mm relation
+ $insert = array(
+ // uid of news item
+ 'uid_local' => $news,
+ // uid of related file
+ 'uid_foreign' => $relatedFile,
+ 'sorting' => $i++
+ );
+ $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_news_domain_model_news_file_mm', $insert);
+
+ // Update related file
+ $update = array(
+ 'parent' => $news
+ );
+ $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_news_domain_model_file', 'uid=' . $relatedFile, $update);
+ }
+ // Update news record
+ $update = array('related_files' => count($relatedFileUids));
+ $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_news_domain_model_news', 'uid=' . $news, $update);
+ }
+ $GLOBALS['TYPO3_DB']->sql_free_result($newsQuery);
+
+ $this->messageArray[] = array(t3lib_FlashMessage::OK, $title, $newsCount . ' news records have been updated!');
+ } else {
+ if ($countNewsWithFileRelation == 0) {
+ $this->messageArray[] = array(t3lib_FlashMessage::NOTICE, $title, 'Nothing to do! There are no news with related files');
+ }
+ if ($countFilesWithParent != 0) {
+ $this->messageArray[] = array(t3lib_FlashMessage::NOTICE, $title, 'Can not update because there are files with new relation model!');
+ }
+ }
+ }
+
+ /**
* Renames a tabled field and does some plausibility checks.
*
* @param string $table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment