/** * Save a model to the database. * * In general, this method fetches the solely property "rows" from the model and updates the local table against * these contents. * * The parent id (id of the model) will get checked and reflected also for new items. * * When rows with duplicate ids are encountered (like from MCW for example), the dupes are inserted as new rows. * * @param InterfaceGeneralModel $objItem The model to save. * * @param bool $recursive Ignored as not relevant in this data provider. * * @return InterfaceGeneralModel The passed Model. * * @throws Exception When the passed model does not contain a property named "rows", an Exception is thrown. */ public function save(InterfaceGeneralModel $objItem, $recursive = false) { $arrData = $objItem->getProperty('rows'); if (!($objItem->getID() && $arrData)) { throw new Exception('invalid input data in model.', 1); } $arrKeep = array(); foreach ($arrData as $i => $arrRow) { // TODO: add an option to restrict this to some allowed fields? $arrSQL = $arrRow; // update all. $intId = intval($arrRow['id']); // Work around the fact that multicolumnwizard does not clear any hidden fields when copying a dataset. // therefore we do consider any dupe as new dataset and save it accordingly. if (in_array($intId, $arrKeep)) { $intId = 0; unset($arrSQL['id']); } if ($intId > 0) { $this->objDatabase->prepare(sprintf('UPDATE tl_metamodel_dca_combine %%s WHERE id=? AND %s=?', $this->strGroupCol))->set($arrSQL)->execute($intId, $objItem->getId()); $arrKeep[] = $intId; } else { // force group col value: $arrSQL[$this->strGroupCol] = $objItem->getId(); $arrKeep[] = $this->objDatabase->prepare('INSERT INTO tl_metamodel_dca_combine %s')->set($arrSQL)->execute()->insertId; } } // house keeping, kill the rest. $this->objDatabase->prepare(sprintf('DELETE FROM tl_metamodel_dca_combine WHERE %s=? AND id NOT IN (%s)', $this->strGroupCol, implode(',', $arrKeep)))->execute($objItem->getId()); return $objItem; }