Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 39 additions & 0 deletions src/MetaModels/DataAccess/DatabaseHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* This file is part of MetaModels/core.
*
* (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.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package MetaModels
* @subpackage Core
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @copyright 2012-2017 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0
* @filesource
*/

namespace MetaModels\DataAccess;

/**
* This trait provides some helper functions for database access.
*/
trait DatabaseHelperTrait
{
/**
* Build a list of the correct amount of "?" for use in a db query.
*
* @param array $parameters The parameters.
*
* @return string
*/
private function buildDatabaseParameterList(array $parameters)
{
return implode(',', array_fill(0, count($parameters), '?'));
}
}
Loading