Last active
August 29, 2015 14:04
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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!'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment