/** * Saves the data changes in the data store. * * @param Enlight_Config $config * @param array $fields * @param bool $update * @return Enlight_Config_Adapter_DbTable */ public function write(Enlight_Config $config, $fields = null, $update = true) { $name = $this->_namePrefix . $config->getName() . $this->_nameSuffix; $section = $config->getSection(); $dbTable = new Enlight_Components_Table(array('name' => $name, 'db' => $this->_db)); $db = $dbTable->getAdapter(); if ($fields === null) { $fields = $config->getDirtyFields(); } if (empty($fields)) { return $this; } $updateData = array(); $insertData = array(); if ($this->_updatedColumn !== null) { $updateData[$this->_updatedColumn] = new Zend_Date(); $insertData[$this->_updatedColumn] = new Zend_Date(); } if ($this->_createdColumn !== null) { $insertData[$this->_createdColumn] = new Zend_Date(); } $where = array(); if ($section !== null) { if (is_array($this->_sectionColumn)) { foreach ($this->_sectionColumn as $key => $sectionColumn) { if (isset($section[$key])) { $where[] = $db->quoteInto($sectionColumn . '=?', $section[$key]); $insertData[$sectionColumn] = $section[$key]; } } } else { $where[] = $db->quoteInto($this->_sectionColumn . '=?', $section); $insertData[$this->_sectionColumn] = $section; } } foreach ((array) $fields as $field) { $fieldWhere = $where; $fieldWhere[] = $db->quoteInto($this->_nameColumn . '=?', $field); $row = $dbTable->fetchRow($fieldWhere); if ($row !== null) { if ($update) { $data = $updateData; if ($this->_automaticSerialization) { $data[$this->_valueColumn] = serialize($config->get($field)); } else { $data[$this->_valueColumn] = $config->get($field); } $dbTable->update($data, $fieldWhere); } } else { $data = $insertData; $data[$this->_nameColumn] = $field; if ($this->_automaticSerialization) { $data[$this->_valueColumn] = serialize($config->get($field)); } else { $data[$this->_valueColumn] = $config->get($field); } $dbTable->insert($data); } } $config->setDirtyFields(array_diff($config->getDirtyFields(), $fields)); return $this; }
/** * Test case */ public function testConfigDirtyFields() { $config = new Enlight_Config(array(), true); $config->set('test', true); $this->assertEquals(array('test'), $config->getDirtyFields()); $config->resetDirtyFields(); $this->assertArrayCount(0, $config->getDirtyFields()); $config->setDirtyFields(array('test')); $this->assertEquals(array('test'), $config->getDirtyFields()); }
/** * Saves the data changes in the data store. * * @param Enlight_Config $config * @param array $fields * @param bool $update If false, existing rows are not updated * @param bool $force If true, existing dirty columns are updated * @param bool $allowReset If true, updating existing columns with existing value will reset dirty flag * @return Enlight_Config_Adapter_DbTable */ public function write(Enlight_Config $config, $fields = null, $update = true, $force = false, $allowReset = false) { if (!$this->_allowWrites) { return $this; } $name = $this->_namePrefix . $config->getName() . $this->_nameSuffix; $section = $config->getSection(); if (is_string($section)) { $section = explode($config->getSectionSeparator(), $config->getSection()); } $dbTable = $this->getTable($this->_namespaceColumn === null ? $name : null); $db = $dbTable->getAdapter(); if ($fields === null) { $fields = $config->getDirtyFields(); } if (empty($fields)) { return $this; } $where = array(); $updateData = array(); $insertData = array(); if ($this->_namespaceColumn !== null) { $insertData[$this->_namespaceColumn] = $name; $where[] = $db->quoteInto($this->_namespaceColumn . '=?', $name); } if ($this->_updatedColumn !== null) { $updateData[$this->_updatedColumn] = new Zend_Date(); $insertData[$this->_updatedColumn] = new Zend_Date(); } if ($this->_createdColumn !== null) { $insertData[$this->_createdColumn] = new Zend_Date(); } if ($section !== null) { if (is_array($this->_sectionColumn)) { foreach ($this->_sectionColumn as $key => $sectionColumn) { if (isset($section[$key])) { $where[] = $db->quoteInto($sectionColumn . '=?', $section[$key]); $insertData[$sectionColumn] = $section[$key]; } } } else { $where[] = $db->quoteInto($this->_sectionColumn . '=?', $section); $insertData[$this->_sectionColumn] = $section; } } foreach ((array) $fields as $field) { $fieldWhere = $where; $fieldWhere[] = $db->quoteInto($this->_nameColumn . '=?', $field); $row = $dbTable->fetchRow($fieldWhere); if ($row !== null) { if ($update) { $data = $updateData; $newValue = $config->get($field); if ($this->_automaticSerialization) { $newValue = serialize($newValue); } if (!$force && $row[$this->_dirtyColumn] == 1 && ($row[$this->_valueColumn] != $newValue || !$allowReset)) { continue; } if ($allowReset && $row[$this->_valueColumn] == $newValue) { $data[$this->_dirtyColumn] = 0; } else { $data[$this->_valueColumn] = $newValue; } $dbTable->update($data, $fieldWhere); } } else { $data = $insertData; $data[$this->_nameColumn] = $field; $data[$this->_dirtyColumn] = 0; if ($this->_automaticSerialization) { $data[$this->_valueColumn] = serialize($config->get($field)); } else { $data[$this->_valueColumn] = $config->get($field); } $dbTable->insert($data); } } $config->setDirtyFields(array_diff($config->getDirtyFields(), $fields)); return $this; }