Skip to content

Commit

Permalink
Hotfix MetaModels#1005 delete empty items
Browse files Browse the repository at this point in the history
  • Loading branch information
zonky2 committed May 19, 2017
1 parent f21ba95 commit e1e6cab
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/MetaModels/Attribute/TranslatedReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This file is part of MetaModels/core.
*
* (c) 2012-2016 The MetaModels team.
* (c) 2012-2017 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -16,7 +16,7 @@
* @author David Maack <david.maack@arcor.de>
* @author Stefan Heimes <stefan_heimes@hotmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2012-2016 The MetaModels team.
* @copyright 2012-2017 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0
* @filesource
*/
Expand Down Expand Up @@ -321,19 +321,30 @@ public function setTranslatedDataFor($arrValues, $strLangCode)
$arrExisting = array_keys($this->getTranslatedDataFor($arrIds, $strLangCode));
$arrNewIds = array_diff($arrIds, $arrExisting);

// Update existing values.
$strQuery = 'UPDATE ' . $this->getValueTable() . ' %s';
// Update existing values - delete if empty.
$strQueryUpdate = 'UPDATE ' . $this->getValueTable() . ' %s';
$strQueryDelete = 'DELETE FROM ' . $this->getValueTable();

foreach ($arrExisting as $intId) {
$arrWhere = $this->getWhere($intId, $strLangCode);
$objDB->prepare($strQuery . ($arrWhere ? ' WHERE ' . $arrWhere['procedure'] : ''))
->set($this->getSetValues($arrValues[$intId], $intId, $strLangCode))
->execute(($arrWhere ? $arrWhere['params'] : null));

if ($arrValues[$intId]['value'] != '') {
$objDB->prepare($strQueryUpdate . ($arrWhere ? ' WHERE ' . $arrWhere['procedure'] : ''))
->set($this->getSetValues($arrValues[$intId], $intId, $strLangCode))
->execute(($arrWhere ? $arrWhere['params'] : null));
} else {
$objDB->prepare($strQueryDelete . ($arrWhere ? ' WHERE ' . $arrWhere['procedure'] : ''))
->execute(($arrWhere ? $arrWhere['params'] : null));
}
}

// Insert the new values.
$strQuery = 'INSERT INTO ' . $this->getValueTable() . ' %s';
$strQueryInsert = 'INSERT INTO ' . $this->getValueTable() . ' %s';
foreach ($arrNewIds as $intId) {
$objDB->prepare($strQuery)
if ($arrValues[$intId]['value'] == '') {
continue;
}
$objDB->prepare($strQueryInsert)
->set($this->getSetValues($arrValues[$intId], $intId, $strLangCode))
->execute();
}
Expand Down

0 comments on commit e1e6cab

Please sign in to comment.