/** * Load version * @param integer $vers * @return boolean * @throws Exception */ public function loadVersion($vers) { $this->rejectChanges(); $versionObject = $this->_model->getStore()->getVersionObjectName(); $vc = Model::factory($versionObject); $data = $vc->getData($this->getName(), $this->getId(), $vers); $pKey = $this->_config->getPrimaryKey(); if (isset($data[$pKey])) { unset($data[$pKey]); } if (empty($data)) { throw new Exception('Cannot load version for ' . $this->getName() . ':' . $this->getId() . '. v:' . $vers); } $iv = false; $ivField = false; if ($this->_config->hasEncrypted()) { $ivField = $this->_config->getIvField(); if (isset($data[$ivField]) && !empty($data[$ivField])) { $iv = base64_decode($data[$ivField]); } } foreach ($data as $k => $v) { if ($this->fieldExists($k)) { if ($this->_config->isMultiLink($k) && !empty($v)) { $v = array_keys($v); } try { if ($this->_config->isEncrypted($k)) { if (!empty($iv)) { $v = $this->_config->decrypt($v, $iv); } } if ($k !== $this->_config->getPrimaryKey() && $k !== 'author_id') { $this->set($k, $v); } } catch (Exception $e) { throw new Exception('Cannot load version data ' . $this->getName() . ':' . $this->getId() . '. v:' . $vers . '. This version contains incompatible data. ' . $e->getMessage()); } } } $this->_version = $vers; }
/** * Prepare list of columns to be updated * * @param Db_Object $object * @return array ( * 'name'=>'somename', * 'action'=>[drop/add/change], * ) */ public function prepareColumnUpdates() { $config = $this->_objectConfig->__toArray(); $updates = array(); if (!$this->tableExists()) { $fields = array(); } else { $fields = $this->_getExistingColumns(); } /* * Remove deprecated fields */ foreach ($fields as $k => $v) { if (!array_key_exists($k, $config['fields'])) { $updates[] = array('name' => $k, 'action' => 'drop', 'type' => 'field'); } } foreach ($config['fields'] as $name => $v) { /* * Add new field */ if (!array_key_exists($name, $fields)) { $updates[] = array('name' => $name, 'action' => 'add'); continue; } $dataType = strtolower($fields[$name]['DATA_TYPE']); /* * Field type compare flag */ $typeCmp = false; /* * Field length compare flag */ $lenCmp = false; /* * IsNull compare flag */ $nullcmp = false; /* * Default value compare flag */ $defaultCmp = false; /* * Unsigned compare flag */ $unsignedCmp = false; /** * AUTO_INCREMENT compare flag * * @var bool */ $incrementCmp = false; if ($v['db_type'] === 'boolean' && $dataType === 'tinyint') { /* * skip check for booleans */ } else { if (strtolower($v['db_type']) !== $dataType) { $typeCmp = true; } if (in_array($v['db_type'], self::$floatTypes, true)) { if ($v['db_scale'] != $fields[$name]['SCALE'] || $v['db_precision'] != $fields[$name]['PRECISION']) { $lenCmp = true; } } elseif (in_array($v['db_type'], self::$numTypes, true) && isset(Db_Object_Property::$numberLength[$v['db_type']])) { $lenCmp = (string) Db_Object_Property::$numberLength[$v['db_type']] != (string) $fields[$name]['LENGTH']; } else { if (isset($v['db_len'])) { $lenCmp = (string) $v['db_len'] != (string) $fields[$name]['LENGTH']; } } /* Auto set default '' for NOT NULL string properties if(in_array($v['db_type'] , self::$charTypes , true) && (! isset($v['db_isNull']) || ! $v['db_isNull']) && (! isset($v['db_default']) || $v['db_default'] === false)) { $v['db_default'] = ''; } */ if (in_array($v['db_type'], self::$textTypes, true)) { if (isset($v['required']) && $v['required']) { $v['db_isNull'] = false; } else { $v['db_isNull'] = true; } } $nullcmp = (bool) $v['db_isNull'] !== (bool) $fields[$name]['NULLABLE']; if ((!isset($v['db_unsigned']) || !$v['db_unsigned']) && $fields[$name]['UNSIGNED']) { $unsignedCmp = true; } if (isset($v['db_unsigned']) && $v['db_unsigned'] && !$fields[$name]['UNSIGNED']) { $unsignedCmp = true; } } if (!(bool) $v['db_isNull'] && !in_array($v['db_type'], self::$dateTypes, true) && !in_array($v['db_type'], self::$textTypes, true)) { if ((!isset($v['db_default']) || $v['db_default'] === false) && !is_null($fields[$name]['DEFAULT'])) { $defaultCmp = true; } if (isset($v['db_default'])) { if (is_null($fields[$name]['DEFAULT']) && $v['db_default'] !== false || !is_null($fields[$name]['DEFAULT']) && $v['db_default'] === false) { $defaultCmp = true; } else { $defaultCmp = (string) $v['db_default'] != (string) $fields[$name]['DEFAULT']; } } } if ($fields[$name]['IDENTITY'] && $name != $this->_objectConfig->getPrimaryKey()) { $incrementCmp = true; } if ($name == $this->_objectConfig->getPrimaryKey() && !$fields[$name]['IDENTITY']) { $incrementCmp = true; } /* * If not passed at least one comparison then rebuild the the field */ if ($typeCmp || $lenCmp || $nullcmp || $defaultCmp || $unsignedCmp || $incrementCmp) { /* * echo $this->_objectName.'<br>'; var_dump($v); * var_dump($fields[$name]); echo 'type - ' . intval($typeCmp)."<br>"; * echo 'len - ' . intval($lenCmp)."<br>"; echo 'null - ' . * intval($nullcmp)."<br>"; echo 'default - ' . * intval($defaultCmp)."<br>"; echo 'unsigned - ' . * intval($unsignedCmp)."<br>"; echo 'increment - ' . * intval($incrementCmp)."<br>"; */ $updates[] = array('name' => $name, 'action' => 'change'); } } return $updates; }
/** * Get primary key name * @return string */ public function getPrimaryKey() { return $this->_objectConfig->getPrimaryKey(); }