/** * Save a single model item. * * @param array $newValues The values to store for a single model item. * @param array $filter If the filter contains old key values these are used * to decide on update versus insert. * @return array The values as they are after saving (they may change). */ protected function _save(array $newValues, array $filter = null) { if ($this->_saveable) { $data = $this->_loadAllTraversable(); if ($data instanceof \Traversable) { $data = iterator_to_array($this->_loadAllTraversable()); } if ($keys = $this->getKeys()) { $search = array(); if (is_array($filter)) { $newValues = $newValues + $filter; } foreach ($keys as $key) { if (isset($newValues[$key])) { $search[$key] = $newValues[$key]; } else { // Crude but hey throw new \MUtil_Model_ModelException(sprintf('Key value "%s" missing when saving data.', $key)); } } $rowId = \MUtil_Ra::findKeys($data, $search); if ($rowId) { // Overwrite to new values $data[$rowId] = $newValues + $data[$rowId]; } else { $data[] = $newValues; } } else { $data[] = $newValues; } $this->_saveAllTraversable($data); return $newValues; } else { throw new \MUtil_Model_ModelException(sprintf('Save not implemented for model "%s".', $this->getName())); } }
public function testFindKeysExistsWrong() { $data = array('row1' => array('c1' => 'a', 'c2' => 'd', 'c3' => 'g', 'c4' => 'j'), 'row2' => array('c1' => 'b', 'c2' => 'e', 'c3' => 'h', 'c4' => 'k'), 'row3' => array('c1' => 'c', 'c2' => 'f', 'c3' => 'i', 'c4' => 'l')); $keys = array('c1' => 'b', 'c3' => 'h'); $this->assertNotEquals(MUtil_Ra::findKeys($data, $keys), 'row3'); }