Пример #1
0
 public function save($forceSave = false)
 {
     if (!$this->isChanged() && !$forceSave) {
         return true;
     }
     $this->_preSave();
     if (!$this->validate()) {
         return false;
     }
     foreach ($this->_dataProcessor->getData() as $columnName => $value) {
         if ($value != $this->_data[$columnName]) {
             $this->_changedData[$columnName] = $value;
         }
     }
     $dataToSave = array();
     foreach ($this->_structure->getColumns() as $column => $info) {
         if (array_key_exists($column, $this->_changedData)) {
             $dataToSave[$column] = $this->_changedData[$column];
         } elseif (array_key_exists('default', $info) && !array_key_exists($column, $this->_data)) {
             $dataToSave[$column] = $info['default'];
         } elseif ($forceSave) {
             if ($column == 'id') {
                 continue;
             }
             $dataToSave[$column] = array_key_exists($column, $this->_data) ? $this->_data[$column] : null;
         }
     }
     $qc = QC::create($this->_tableName);
     $dataToSave = $this->getPackedData($dataToSave);
     if ($this->_isNew) {
         if (empty($dataToSave)) {
             $dataToSave = array($this->_primaryKey => null);
         }
         $this->{$this->_primaryKey} = $qc->insert($dataToSave)->execute();
     } elseif (count($dataToSave)) {
         $qc->update($dataToSave)->where(array($this->_primaryKey => $this->{$this->_primaryKey}))->execute();
     }
     /**
      * Reloading data from DB for the newly created object
      */
     if ($this->_isNew) {
         $data = QC::create($this->_tableName)->where(array($this->_tableName . '.' . $this->_primaryKey => $this->{$this->_primaryKey}))->executeOne();
         $this->setOriginalData($data);
         $this->unpackOriginalData();
     }
     $this->_postSave();
     $this->_isNew = false;
     $this->_changedData = array();
     return true;
 }
Пример #2
0
 public function testBasic()
 {
     $simpleData = array('login' => '   login with space   ');
     $dp = DataProcessor::createInstance();
     $dp->addProcessRule('login', 'trim');
     $dp->process($simpleData);
     $this->assertEquals('login with space', $dp->getData('login'), 'trim process loaded and worked');
     $dp->addValidationRule('login', 'email');
     $dp->process($simpleData);
     $this->assertArrayHasKey('login', $dp->getErrors(), 'Has validation error on email');
     $data = array('login' => '  a@viniychuk.com ', 'name' => '  Alexandr');
     $dp->process($data);
     $this->assertTrue($dp->isValid(), 'Valid email');
     $dp->clearProcessRules();
     $dp->clearValidationRules();
     $allRules = array('name' => array(array('email'), array('trim')));
     $result = DataProcessor::createInstance($allRules)->process(array('name' => 'data  '));
     $this->assertNotEmpty($result->getErrors(), 'Errors on grouped rules');
     $this->assertEquals('data', $result->getData('name'), 'Processing on grouped rules');
     $dp = new DataProcessor();
     $dp->addValidationRule('name', 'mandatory');
     $this->assertNotEmpty($dp->process(array('login' => 'asdf'))->getErrors(), 'Mandatory rule');
 }