From: <gem...@li...> - 2012-11-15 14:18:53
|
Revision: 1017 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=1017&view=rev Author: matijsdejong Date: 2012-11-15 14:18:41 +0000 (Thu, 15 Nov 2012) Log Message: ----------- Made it possible to declare certain tables in a join model to be update or insert only Modified Paths: -------------- trunk/library/classes/Gems/Model/JoinModel.php trunk/library/classes/Gems/Model/RespondentModel.php trunk/library/classes/Gems/Model.php trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php trunk/library/classes/MUtil/Model/FormBridge.php trunk/library/classes/MUtil/Model/JoinModel.php Modified: trunk/library/classes/Gems/Model/JoinModel.php =================================================================== --- trunk/library/classes/Gems/Model/JoinModel.php 2012-11-13 12:48:38 UTC (rev 1016) +++ trunk/library/classes/Gems/Model/JoinModel.php 2012-11-15 14:18:41 UTC (rev 1017) @@ -49,14 +49,14 @@ /** * Create a model that joins two or more tables * - * @param string $name the name of the model - * @param string $startTable The base table for the model - * @param string $fieldPrefix Prefix to use for change fields (date/userid) - * @param bool $saveable Will changes to this table be saved + * @param string $name A name for the model + * @param string $startTable The base table for the model + * @param string $fieldPrefix Prefix to use for change fields (date/userid), if $saveable empty sets it to true + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants */ public function __construct($name, $startTable, $fieldPrefix = null, $saveable = null) { - parent::__construct($name, $startTable, (null === $saveable ? $fieldPrefix : $saveable)); + parent::__construct($name, $startTable, $this->_checkSaveable($saveable, $fieldPrefix)); if ($fieldPrefix) { Gems_Model::setChangeFieldsByPrefix($this, $fieldPrefix); @@ -64,18 +64,33 @@ } /** + * + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants + * @param string $fieldPrefix Prefix to use for change fields (date/userid), if $saveable empty sets it to true + * @return mixed The saveable setting to use + */ + protected function _checkSaveable($saveable, $fieldPrefix) + { + if (null === $saveable) { + return $fieldPrefix ? parent::SAVE_MODE_ALL : null; + } + + return $saveable; + } + + /** * Add a table to the model with a left join * - * @param string $table The name of the table to join - * @param array $joinFields Array of source->dest primary keys for this join - * @param string $fieldPrefix Prefix to use for change fields (date/userid) - * @param bool $saveable Will changes to this table be saved + * @param string $table The name of the table to join + * @param array $joinFields Array of source->dest primary keys for this join + * @param string $fieldPrefix Prefix to use for change fields (date/userid), if $saveable empty sets it to true + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants * * @return Gems_Model_JoinModel */ public function addLeftTable($table, array $joinFields, $fieldPrefix = null, $saveable = null) { - parent::addLeftTable($table, $joinFields, (null === $saveable ? $fieldPrefix : $saveable)); + parent::addLeftTable($table, $joinFields, $this->_checkSaveable($saveable, $fieldPrefix)); if ($fieldPrefix) { Gems_Model::setChangeFieldsByPrefix($this, $fieldPrefix); @@ -87,16 +102,16 @@ /** * Add a table to the model with a right join * - * @param string $table The name of the table to join - * @param array $joinFields Array of source->dest primary keys for this join - * @param string $fieldPrefix Prefix to use for change fields (date/userid) - * @param bool $saveable Will changes to this table be saved + * @param string $table The name of the table to join + * @param array $joinFields Array of source->dest primary keys for this join + * @param string $fieldPrefix Prefix to use for change fields (date/userid), if $saveable empty sets it to true + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants * * @return Gems_Model_JoinModel */ public function addRightTable($table, array $joinFields, $fieldPrefix = null, $saveable = null) { - parent::addRightTable($table, $joinFields, (null === $saveable ? $fieldPrefix : $saveable)); + parent::addRightTable($table, $joinFields, $this->_checkSaveable($saveable, $fieldPrefix)); if ($fieldPrefix) { Gems_Model::setChangeFieldsByPrefix($this, $fieldPrefix); @@ -108,16 +123,16 @@ /** * Add a table to the model with an inner join * - * @param string $table The name of the table to join - * @param array $joinFields Array of source->dest primary keys for this join - * @param string $fieldPrefix Prefix to use for change fields (date/userid) - * @param bool $saveable Will changes to this table be saved + * @param string $table The name of the table to join + * @param array $joinFields Array of source->dest primary keys for this join + * @param string $fieldPrefix Prefix to use for change fields (date/userid), if $saveable empty sets it to true + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants * * @return Gems_Model_JoinModel */ public function addTable($table, array $joinFields, $fieldPrefix = null, $saveable = null) { - parent::addTable($table, $joinFields, (null === $saveable ? $fieldPrefix : $saveable)); + parent::addTable($table, $joinFields, $this->_checkSaveable($saveable, $fieldPrefix)); if ($fieldPrefix) { Gems_Model::setChangeFieldsByPrefix($this, $fieldPrefix); @@ -127,14 +142,14 @@ /** * - * @param string $table_name Does not test for existence - * @param string $fieldPrefix Prefix to use for change fields (date/userid) - * @param bool $saveable Will changes to this table be saved + * @param string $table_name Does not test for existence + * @param string $fieldPrefix Prefix to use for change fields (date/userid), if $saveable empty sets it to true + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants * @return Gems_Model_JoinModel */ public function setTableSaveable($table_name, $fieldPrefix = null, $saveable = null) { - parent::setTableSaveable($table_name, (null === $saveable ? $fieldPrefix : $saveable)); + parent::setTableSaveable($table_name, $this->_checkSaveable($saveable, $fieldPrefix)); if ($fieldPrefix) { Gems_Model::setChangeFieldsByPrefix($this, $fieldPrefix); Modified: trunk/library/classes/Gems/Model/RespondentModel.php =================================================================== --- trunk/library/classes/Gems/Model/RespondentModel.php 2012-11-13 12:48:38 UTC (rev 1016) +++ trunk/library/classes/Gems/Model/RespondentModel.php 2012-11-15 14:18:41 UTC (rev 1017) @@ -128,9 +128,17 @@ */ public function addLoginCheck() { - $this->addLeftTable('gems__user_logins', array('gr2o_patient_nr' => 'gul_login', 'gr2o_id_organization' => 'gul_id_organization'), 'gul'); - $this->addColumn("CASE WHEN gul_id_user IS NULL OR gul_user_class = 'NoLogin' OR gul_can_login = 0 THEN 0 ELSE 1 END", 'has_login'); + $this->addLeftTable( + 'gems__user_logins', + array('gr2o_patient_nr' => 'gul_login', 'gr2o_id_organization' => 'gul_id_organization'), + 'gul', + MUtil_Model_DatabaseModelAbstract::SAVE_MODE_UPDATE | + MUtil_Model_DatabaseModelAbstract::SAVE_MODE_DELETE); + $this->addColumn( + "CASE WHEN gul_id_user IS NULL OR gul_user_class = 'NoLogin' OR gul_can_login = 0 THEN 0 ELSE 1 END", + 'has_login'); + return $this; } Modified: trunk/library/classes/Gems/Model.php =================================================================== --- trunk/library/classes/Gems/Model.php 2012-11-13 12:48:38 UTC (rev 1016) +++ trunk/library/classes/Gems/Model.php 2012-11-15 14:18:41 UTC (rev 1017) @@ -104,7 +104,13 @@ */ protected function addUserLogin(Gems_Model_JoinModel $model, $loginField, $organizationField) { - $model->addTable('gems__user_logins', array($loginField => 'gul_login', $organizationField => 'gul_id_organization'), 'gul'); + $model->addTable( + 'gems__user_logins', + array($loginField => 'gul_login', $organizationField => 'gul_id_organization'), + 'gul', + MUtil_Model_DatabaseModelAbstract::SAVE_MODE_UPDATE | + MUtil_Model_DatabaseModelAbstract::SAVE_MODE_DELETE + ); } /** Modified: trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php =================================================================== --- trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php 2012-11-13 12:48:38 UTC (rev 1016) +++ trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php 2012-11-15 14:18:41 UTC (rev 1017) @@ -62,6 +62,30 @@ const KEY_COPIER = '__c_1_3_copy__%s__key_k_0_p_1__'; /** + * Default save mode: execute all saves + */ + const SAVE_MODE_ALL = 7; + + /** + * Allow deletes to be executed + */ + const SAVE_MODE_DELETE = 4; + + /** + * Allow inserts to be executed + */ + const SAVE_MODE_INSERT = 2; + + /** + * Allow updates to be executed + */ + const SAVE_MODE_UPDATE = 1; + + /** + * Do nothing + */ + const SAVE_MODE_NONE = 0; + /** * Name for query filter transformers */ const TEXTFILTER_TRANSFORMER = 'filter_transformer'; @@ -257,7 +281,6 @@ } } } - return $this->_filterDataForSave($tableData, $isNew); } @@ -373,142 +396,148 @@ * @see copyKeys() * * @param Zend_Db_Table_Abstract $table The table to save - * @param array $newValues The values to save, including those for other tables - * @param array $oldKeys The original keys as they where before the changes + * @param array $newValues The values to save, including those for other tables + * @param array $oldKeys The original keys as they where before the changes + * @param int $saveMode Should updates / inserts occur * @return array The values for this table as they were updated */ - protected function _saveTableData(Zend_Db_Table_Abstract $table, array $newValues, array $oldKeys = null) + protected function _saveTableData(Zend_Db_Table_Abstract $table, array $newValues, + array $oldKeys = null, $saveMode = self::SAVE_MODE_ALL) { - if ($newValues) { - $table_name = $this->_getTableName($table); - $primaryKeys = $this->_getKeysFor($table_name); - $primaryCount = count($primaryKeys); - $filter = array(); - $update = true; + if (! $newValues) { + return array(); + } - // MUtil_Echo::r($newValues, $table_name); + $table_name = $this->_getTableName($table); + $primaryKeys = $this->_getKeysFor($table_name); + $primaryCount = count($primaryKeys); + $filter = array(); + $update = true; - foreach ($primaryKeys as $key) { - if (array_key_exists($key, $newValues) && (0 == strlen($newValues[$key]))) { - // Never include null key values - unset($newValues[$key]); - if (MUtil_Model::$verbose) { - MUtil_Echo::r('Null key value: ' . $key, 'INSERT!!'); - } + // MUtil_Echo::r($newValues, $table_name); + foreach ($primaryKeys as $key) { + if (array_key_exists($key, $newValues) && (0 == strlen($newValues[$key]))) { + // Never include null key values + unset($newValues[$key]); + if (MUtil_Model::$verbose) { + MUtil_Echo::r('Null key value: ' . $key, 'INSERT!!'); + } - // Now we know we are not updating - $update = false; + // Now we know we are not updating + $update = false; - } elseif (isset($oldKeys[$key])) { + } elseif (isset($oldKeys[$key])) { + if (MUtil_Model::$verbose) { + MUtil_Echo::r($key . ' => ' . $oldKeys[$key], 'Old key'); + } + $filter[$key . ' = ?'] = $oldKeys[$key]; + // Key values left in $returnValues in case of partial key insert + + } else { + // Check for old key values being stored using copyKeys() + $copyKey = $this->getKeyCopyName($key); + + if (isset($newValues[$copyKey])) { + $filter[$key . ' = ?'] = $newValues[$copyKey]; if (MUtil_Model::$verbose) { - MUtil_Echo::r($key . ' => ' . $oldKeys[$key], 'Old key'); + MUtil_Echo::r($key . ' => ' . $newValues[$copyKey], 'Copy key'); } - $filter[$key . ' = ?'] = $oldKeys[$key]; - // Key values left in $returnValues in case of partial key insert - } else { - // Check for old key values being stored using copyKeys() - $copyKey = $this->getKeyCopyName($key); - - if (isset($newValues[$copyKey])) { - $filter[$key . ' = ?'] = $newValues[$copyKey]; - if (MUtil_Model::$verbose) { - MUtil_Echo::r($key . ' => ' . $newValues[$copyKey], 'Copy key'); - } - - } elseif (isset($newValues[$key])) { - $filter[$key . ' = ?'] = $newValues[$key]; - if (MUtil_Model::$verbose) { - MUtil_Echo::r($key . ' => ' . $newValues[$key], 'Key'); - } + } elseif (isset($newValues[$key])) { + $filter[$key . ' = ?'] = $newValues[$key]; + if (MUtil_Model::$verbose) { + MUtil_Echo::r($key . ' => ' . $newValues[$key], 'Key'); } } } + } + if (! $filter) { + $update = false; + } - if ($update) { - // MUtil_Echo::r($filter, 'Filter'); + if ($update) { + // MUtil_Echo::r($filter, 'Filter'); - $adapter = $this->getAdapter(); - $wheres = array(); - foreach ($filter as $text => $value) { - $wheres[] = $adapter->quoteInto($text, $value); - } - // Retrieve the record from the database - $oldValues = $table->fetchRow('(' . implode(' ) AND (', $wheres) . ')'); + $adapter = $this->getAdapter(); + $wheres = array(); + foreach ($filter as $text => $value) { + $wheres[] = $adapter->quoteInto($text, $value); + } + // Retrieve the record from the database + $oldValues = $table->fetchRow('(' . implode(' ) AND (', $wheres) . ')'); + if (! $oldValues) { + // MUtil_Echo::r('INSERT!!', 'Old not found'); + // Apparently the record does not exist in the database + $update = false; + } else { + $oldValues = $oldValues->toArray(); + } + } - if (! $oldValues) { - // MUtil_Echo::r('INSERT!!', 'Old not found'); - // Apparently the record does not exist in the database - $update = false; - } else { - $oldValues = $oldValues->toArray(); - } + // Check for actual values for this table to save. + // MUtil_Echo::track($newValues); + if ($returnValues = $this->_filterDataFor($table_name, $newValues, ! $update)) { + if (true || MUtil_Model::$verbose) { + MUtil_Echo::r($returnValues, 'Return'); } + // MUtil_Echo::track($returnValues); - // Check for actual values for this table to save. - // MUtil_Echo::track($newValues); - if ($returnValues = $this->_filterDataFor($table_name, $newValues, ! $update)) { - if (MUtil_Model::$verbose) { - MUtil_Echo::r($returnValues, 'Return'); - } - // MUtil_Echo::track($returnValues); + if ($update) { + // MUtil_Echo::r($filter); - if ($update) { - // MUtil_Echo::r($filter); + // Check for actual changes + foreach ($oldValues as $name => $value) { - // Check for actual changes - foreach ($oldValues as $name => $value) { + // The name is in the set being stored + if (array_key_exists($name, $returnValues)) { - // The name is in the set being stored - if (array_key_exists($name, $returnValues)) { + // Detect change that is not auto update + if (! (($returnValues[$name] == $value) || $this->isAutoSave($name))) { + // MUtil_Echo::rs($name, $returnValues[$name], $value); + // MUtil_Echo::r($returnValues); - // Detect change that is not auto update - if (! (($returnValues[$name] == $value) || $this->isAutoSave($name))) { - // MUtil_Echo::rs($name, $returnValues[$name], $value); - // MUtil_Echo::r($returnValues); + // Update the row, if the saveMode allows it + if (($saveMode & self::SAVE_MODE_UPDATE) && + $changed = $table->update($returnValues, $filter)) { + $this->addChanged($changed); + // Make sure the copy keys (if any) have the new values as well + $returnValues = $this->_updateCopyKeys($primaryKeys, $returnValues); - // Update the row - if ($changed = $table->update($returnValues, $filter)) { - $this->addChanged($changed); - // Make sure the copy keys (if any) have the new values as well - $returnValues = $this->_updateCopyKeys($primaryKeys, $returnValues); + // Add the old values as we have them and they may be of use later on. + $returnValues = $returnValues + $oldValues; - // Add the old values as we have them and they may be of use later on. - $returnValues = $returnValues + $oldValues; - - return $returnValues; - } + return $returnValues; } } } - // Add the old values as we have them and they may be of use later on. - return $returnValues + $oldValues; + } + // Add the old values as we have them and they may be of use later on. + return $returnValues + $oldValues; + } elseif ($saveMode & self::SAVE_MODE_INSERT) { + // Perform insert + // MUtil_Echo::r($returnValues); + $newKeyValues = $table->insert($returnValues); + $this->addChanged(); + // MUtil_Echo::rs($newKeyValues, $primaryKeys); + + // Composite key returned. + if (is_array($newKeyValues)) { + foreach ($newKeyValues as $key => $value) { + $returnValues[$key] = $value; + } + return $this->_updateCopyKeys($primaryKeys, $returnValues); } else { - // Perform insert - // MUtil_Echo::r($returnValues); - $newKeyValues = $table->insert($returnValues); - $this->addChanged(); - // MUtil_Echo::rs($newKeyValues, $primaryKeys); - - // Composite key returned. - if (is_array($newKeyValues)) { - foreach ($newKeyValues as $key => $value) { - $returnValues[$key] = $value; + // Single key returned + foreach ($primaryKeys as $key) { + // Fill the first empty value + if (! isset($returnValues[$key])) { + $returnValues[$key] = $newKeyValues; + return $this->_updateCopyKeys($primaryKeys, $returnValues); } - return $this->_updateCopyKeys($primaryKeys, $returnValues); - } else { - // Single key returned - foreach ($primaryKeys as $key) { - // Fill the first empty value - if (! isset($returnValues[$key])) { - $returnValues[$key] = $newKeyValues; - return $this->_updateCopyKeys($primaryKeys, $returnValues); - } - } - // But if all the key values were already filled, make sure the new values are returned. - return $this->_updateCopyKeys($primaryKeys, $returnValues); } + // But if all the key values were already filled, make sure the new values are returned. + return $this->_updateCopyKeys($primaryKeys, $returnValues); } } } Modified: trunk/library/classes/MUtil/Model/FormBridge.php =================================================================== --- trunk/library/classes/MUtil/Model/FormBridge.php 2012-11-13 12:48:38 UTC (rev 1016) +++ trunk/library/classes/MUtil/Model/FormBridge.php 2012-11-15 14:18:41 UTC (rev 1017) @@ -245,7 +245,7 @@ // First strip the options that are not allowed if (MUtil_Model::$verbose) { - $strippedKeys = array_keys(array_diff_key($options, $allowedOptionsflipped)); + $strippedKeys = array_keys(array_diff_key($options, $allowedOptionsFlipped)); if (!empty($strippedKeys)) { MUtil_Echo::r($strippedKeys, 'stripped from options for ' . $name); } @@ -349,7 +349,7 @@ // Make sure form knows it is a jQuery form $this->form->activateJQuery(); - + return $this->_addToForm($name, 'DatePicker', $options); } @@ -379,14 +379,14 @@ $options = func_get_args(); $options = MUtil_Ra::pairs($options, 2); - //MUtil_Echo::track($elements); + // MUtil_Echo::track($elements); if (isset($elements['elements'])) { - MUtil_Echo::track($elements, $options); + // MUtil_Echo::track($elements, $options); $tmpElements = $elements['elements']; unset($elements['elements']); $options = $elements + $options; $elements = $tmpElements; - //MUtil_Echo::track($elements, $options); + // MUtil_Echo::track($elements, $options); } $options = $this->_mergeOptions($name, $options, Modified: trunk/library/classes/MUtil/Model/JoinModel.php =================================================================== --- trunk/library/classes/MUtil/Model/JoinModel.php 2012-11-13 12:48:38 UTC (rev 1016) +++ trunk/library/classes/MUtil/Model/JoinModel.php 2012-11-15 14:18:41 UTC (rev 1017) @@ -60,9 +60,9 @@ /** * Create a model that joins two or more tables * - * @param string $name the name of the model - * @param string $startTable The base table for the model - * @param bool $saveable Will changes to this table be saved + * @param string $name A name for the model + * @param string $startTable The base table for the model + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants */ public function __construct($name, $startTable, $saveable = false) { @@ -77,6 +77,40 @@ $this->_select->from($this->_getTableName($table), array()); } + /** + * Check the passed saveTable information and return 'new style' SAVE_MODE + * constant array + * + * @param array $saveTables Optional array containing the table names to save, + * otherwise the tables set to save at model level will be saved. + * @return array Containing savetable data + */ + protected function _checkSaveTables($saveTables) + { + if (null === $saveTables) { + return $this->_saveTables; + } + + $results = array(); + foreach ((array) $saveTables as $tableName => $setting) { + if (is_numeric($tableName) || (true === $setting)) { + $results[$setting] = self::SAVE_MODE_ALL; + } elseif ($setting) { + $results[$tableName] = $setting; + } + } + + return $results; + } + + /** + * Join a table to the select statement and load the table information + * + * @param string $join Join function name specifying the type of join + * @param mixed $table The name of the table to join or a table object + * @param array $joinFields Array of field pairs that form the join statement + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants + */ protected function _joinTable($join, $table, array $joinFields, $saveable = false) { $table = $this->_loadTable($table, $saveable); @@ -91,32 +125,58 @@ $this->_select->$join($table_name, implode(' ' . Zend_Db_Select::SQL_AND . ' ', $joinSql), array()); } + /** + * Load table meta data and set the models table properties + * + * @param mixed $table The name of the table to join or a table object + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants + * @return Zend_DB_Table + */ protected function _loadTable($table, $saveable = false) { if ($table instanceof Zend_Db_Table_Abstract) { - $table_name = $this->_getTableName($table); + $tableName = $this->_getTableName($table); } else { - $table_name = (string) $table; - $table = new Zend_DB_Table($table_name); + $tableName = (string) $table; + $table = new Zend_DB_Table($tableName); } - $this->_tables[$table_name] = $table; + $this->_tables[$tableName] = $table; - if ($saveable) { - $this->_saveTables[] = $table_name; - } - + $this->_setTableSaveable($tableName, $saveable); $this->_loadTableMetaData($table); return $table; } /** + * Add the table to the default save tables. + * + * This private functions saves against overloading + * + * Only tables marked as save tables are saved during a save() or delete(), + * unless this is overuled by the extra parameter for those functions in + * this object. + * + * @param string $tableName Does not test for existence + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants + */ + private function _setTableSaveable($tableName, $saveable) + { + if (true === $saveable) { + $this->_saveTables[$tableName] = self::SAVE_MODE_ALL; + } elseif ($saveable) { + $this->_saveTables[$tableName] = $saveable; + } else { + unset($this->_saveTables[$tableName]); + } + } + + /** * Add a table to the model with a left join * - * @param string $table The name of the table to join - * @param array $joinFields Array of source->dest primary keys for this join - * @param bool $saveable Will changes to this table be saved - * + * @param string $table The name of the table to join + * @param array $joinFields Array of source->dest primary keys for this join + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants * @return MUtil_Model_JoinModel */ public function addLeftTable($table, array $joinFields, $saveable = false) @@ -128,9 +188,9 @@ /** * Add a table to the model with a right join * - * @param string $table The name of the table to join - * @param array $joinFields Array of source->dest primary keys for this join - * @param bool $saveable Will changes to this table be saved + * @param string $table The name of the table to join + * @param array $joinFields Array of source->dest primary keys for this join + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants * * @return MUtil_Model_JoinModel */ @@ -143,9 +203,9 @@ /** * Add a table to the model with an inner join * - * @param string $table The name of the table to join - * @param array $joinFields Array of source->dest primary keys for this join - * @param bool $saveable Will changes to this table be saved + * @param string $table The name of the table to join + * @param array $joinFields Array of source->dest primary keys for this join + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants * * @return Gems_Model_JoinModel */ @@ -169,10 +229,8 @@ */ public function delete($filter = true, array $saveTables = null) { - if (null === $saveTables) { - $saveTables = $this->_saveTables; - } - $filter = $this->_checkFilterUsed($filter); + $saveTables = $this->_checkSaveTables($saveTables); + $filter = $this->_checkFilterUsed($filter); if ($this->_deleteValues) { // First get the old values so we can have all the key values @@ -182,12 +240,12 @@ $changed = $this->save($this->_deleteValues + $oldValues, $filter, $saveTables); } else { $changed = 0; - foreach ($saveTables as $table_name) { + foreach ($saveTables as $tableName => $saveMode) { $table_filter = array(); - $delete = true; + $delete = $saveMode & self::SAVE_MODE_DELETE; // Find per table key filters - foreach ($this->_getKeysFor($table_name) as $key) { + foreach ($this->_getKeysFor($tableName) as $key) { if (isset($filter[$key])) { $table_filter[$key] = $filter[$key]; } else { @@ -213,7 +271,7 @@ // MUtil_Echo::r($table_filter, $table_name); if ($delete && $table_filter) { - $changed = max($changed, $this->_deleteTableData($this->_tables[$table_name], $table_filter)); + $changed = max($changed, $this->_deleteTableData($this->_tables[$tableName], $table_filter)); } } } @@ -253,16 +311,13 @@ */ public function save(array $newValues, array $filter = null, array $saveTables = null) { - if (null === $saveTables) { - $saveTables = $this->_saveTables; - } - + $saveTables = $this->_checkSaveTables($saveTables); $oldChanged = $this->getChanged(); // MUtil_Echo::track($newValues, $filter, $saveTables, $this->_joinFields); $oldValues = $newValues; - foreach ($saveTables as $table_name) { + foreach ($saveTables as $tableName => $saveMode) { // Gotta repeat this every time, as keys may be set later foreach ($this->_joinFields as $source => $target) { // Use is_string as $target and $target can be e.g. a Zend_Db_Expr() object @@ -280,7 +335,9 @@ } elseif (! (isset($newValues[$source]) && $newValues[$source])) { $newValues[$source] = $newValues[$target]; - } elseif ((strlen($newValues[$target]) > 0) && (strlen($newValues[$source]) > 0) && $newValues[$target] != $newValues[$source]) { + } elseif ((strlen($newValues[$target]) > 0) && + (strlen($newValues[$source]) > 0) && + $newValues[$target] != $newValues[$source]) { // Join key values changed. // // Set the old values as the filter @@ -293,13 +350,15 @@ // The changing field must be stated first in the join statement. $newValues[$target] = $newValues[$source]; } - } elseif ($target instanceof Zend_Db_Expr && (! (isset($newValues[$source]) && $newValues[$source]))) { + } elseif ($target instanceof Zend_Db_Expr && + (! (isset($newValues[$source]) && $newValues[$source]))) { $newValues[$source] = $target; } } //$this->_saveTableData returns the new row values, including any automatic changes. - $newValues = $this->_saveTableData($this->_tables[$table_name], $newValues, $filter) + $oldValues; + $newValues = $this->_saveTableData($this->_tables[$tableName], $newValues, $filter, $saveMode) + + $oldValues; // MUtil_Echo::track($oldValues, $newValues, $filter); $oldValues = $newValues; } @@ -345,22 +404,13 @@ * unless this is overuled by the extra parameter for those functions in * this object. * - * @param string $table_name Does not test for existence - * @param boolean $saveable Will changes to this table be saved + * @param string $tableName Does not test for existence + * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants * @return MUtil_Model_JoinModel (continuation pattern) */ - public function setTableSaveable($table_name, $saveable = true) + public function setTableSaveable($tableName, $saveable = true) { - // MUtil_Echo::r(func_get_args(), __CLASS__ . '->' . __FUNCTION__); - if ($saveable) { - if (! in_array($table_name, $this->_saveTables)) { - $this->_saveTables[] = $table_name; - } - } else { - $key = array_search($table_name, $this->_saveTables); - unset($this->_saveTables[$key]); - } - + $this->_setTableSaveable($tableName, $saveable); return $this; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |