Esempio n. 1
0
 /**
  * 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;
 }
Esempio n. 2
0
 /**
  * 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());
 }
Esempio n. 3
0
 /**
  * Removes the data from the data store.
  *
  * @param      Enlight_Config $config
  * @param      array          $fields
  * @param      bool           $update
  * @return     Enlight_Config_Adapter_DbTable
  */
 public function delete(Enlight_Config $config, $fields = null, $deleteDirty = false)
 {
     $name = $this->_namePrefix . $config->getName() . $this->_nameSuffix;
     $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();
     $insertData = array();
     if ($this->_namespaceColumn !== null) {
         $insertData[$this->_namespaceColumn] = $name;
         $where[] = $db->quoteInto($this->_namespaceColumn . '=?', $name);
     }
     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);
         if (!$deleteDirty) {
             $fieldWhere[] = $db->quoteInto($this->_dirtyColumn . '=?', 0);
         }
         $row = $dbTable->fetchRow($fieldWhere);
         if ($row !== null) {
             $row->delete();
         }
     }
     return $this;
 }