diff --git a/src/Pluf/DB.php b/src/Pluf/DB.php index 166dd85..e99391f 100644 --- a/src/Pluf/DB.php +++ b/src/Pluf/DB.php @@ -147,7 +147,7 @@ function Pluf_DB_IdentityFromDb($val) */ function Pluf_DB_IdentityToDb($val, $db) { - if (is_null($val)) { + if (null === $val) { return 'NULL'; } return $db->esc($val); @@ -163,7 +163,7 @@ function Pluf_DB_SerializedFromDb($val) function Pluf_DB_SerializedToDb($val, $db) { - if (is_null($val)) { + if (null === $val) { return 'NULL'; } return $db->esc(serialize($val)); @@ -176,7 +176,7 @@ function Pluf_DB_CompressedFromDb($val) function Pluf_DB_CompressedToDb($val, $db) { - return (is_null($val)) ? 'NULL' : $db->esc(gzdeflate($val, 9)); + return (null === $val) ? 'NULL' : $db->esc(gzdeflate($val, 9)); } function Pluf_DB_BooleanFromDb($val) { @@ -187,7 +187,7 @@ function Pluf_DB_BooleanFromDb($val) { } function Pluf_DB_BooleanToDb($val, $db) { - if (is_null($val)) { + if (null === $val) { return 'NULL'; } if ($val) { @@ -197,15 +197,11 @@ function Pluf_DB_BooleanToDb($val, $db) { } function Pluf_DB_IntegerFromDb($val) { - if (is_null($val)) return null; - return (int) $val; + return (null === $val) ? null : (int) $val; } function Pluf_DB_IntegerToDb($val, $db) { - if (is_null($val)) { - return 'NULL'; - } - return (string)(int)$val; + return (null === $val) ? 'NULL' : (string)(int)$val; } function Pluf_DB_PasswordToDb($val, $db) { diff --git a/src/Pluf/DB/Field.php b/src/Pluf/DB/Field.php index 4325de8..1697dc6 100644 --- a/src/Pluf/DB/Field.php +++ b/src/Pluf/DB/Field.php @@ -63,7 +63,9 @@ class Pluf_DB_Field { $this->value = $value; $this->column = $column; - $this->extra = array_merge($this->extra, $extra); + if ($extra) { + $this->extra = array_merge($this->extra, $extra); + } } /** diff --git a/src/Pluf/Model.php b/src/Pluf/Model.php index df3fd37..71c24c4 100644 --- a/src/Pluf/Model.php +++ b/src/Pluf/Model.php @@ -290,10 +290,16 @@ class Pluf_Model */ function _getConnection() { + static $con = null; if ($this->_con !== null) { return $this->_con; } + if ($con !== null) { + $this->_con = $con; + return $this->_con; + } $this->_con = &Pluf::db($this); + $con = $this->_con; return $this->_con; } @@ -323,12 +329,8 @@ class Pluf_Model */ function __get($prop) { - if (array_key_exists($prop, $this->_data)) return $this->_data[$prop]; - else try { - return $this->__call($prop, array()); - } catch (Exception $e) { - throw new Exception(sprintf('Cannot get property "%s".', $prop)); - } + return (array_key_exists($prop, $this->_data)) ? + $this->_data[$prop] : $this->__call($prop, array()); } /** @@ -339,7 +341,7 @@ class Pluf_Model */ function __set($prop, $val) { - if (!is_null($val) and isset($this->_cache['fk'][$prop])) { + if (null !== $val and isset($this->_cache['fk'][$prop])) { $this->_data[$prop] = $val->id; unset($this->_cache['get_'.$prop]); } else { @@ -737,41 +739,53 @@ class Pluf_Model /** * Create the model into the database. + * + * If raw insert is requested, the preSave/postSave methods are + * not called and the current id of the object is directly + * used. This is particularily used when doing backup/restore of + * data. * + * @param bool Raw insert (false) * @return bool Success */ - function create($force_id=false) + function create($raw=false) { - $this->preSave(true); + if (!$raw) { + $this->preSave(true); + } $req = 'INSERT INTO '.$this->getSqlTable()."\n"; $icols = array(); $ivals = array(); $assoc = array(); foreach ($this->_a['cols'] as $col=>$val) { $field = new $val['type'](); - if ($col == 'id' and !$force_id) { + if ($col == 'id' and !$raw) { continue; } elseif ($field->type == 'manytomany') { // If is a defined array, we need to associate. - if (is_array($this->$col)) { - $assoc[$val['model']] = $this->$col; + if (is_array($this->_data[$col])) { + $assoc[$val['model']] = $this->_data[$col]; } continue; } $icols[] = $this->_con->qn($col); - $ivals[] = $this->_toDb($this->$col, $col); + $ivals[] = $this->_toDb($this->_data[$col], $col); } $req .= '('.implode(', ', $icols).') VALUES '; $req .= '('.implode(','."\n", $ivals).')'; $this->_con->execute($req); - if (false === ($id=$this->_con->getLastID())) { - throw new Exception($this->_con->getError()); + if (!$raw) { + if (false === ($id=$this->_con->getLastID())) { + throw new Exception($this->_con->getError()); + } + $this->_data['id'] = $id; } - $this->_data['id'] = $id; foreach ($assoc as $model=>$ids) { $this->batchAssoc($model, $ids); } - $this->postSave(true); + if (!$raw) { + $this->postSave(true); + } return true; } @@ -956,7 +970,7 @@ class Pluf_Model function _fromDb($val, $col) { $m = $this->_con->type_cast[$this->_a['cols'][$col]['type']][0]; - return $m($val); + return ($m == 'Pluf_DB_IdentityFromDb') ? $val : $m($val); } /** diff --git a/src/Pluf/Test/Fixture.php b/src/Pluf/Test/Fixture.php index 209bbcc..d53b72a 100644 --- a/src/Pluf/Test/Fixture.php +++ b/src/Pluf/Test/Fixture.php @@ -40,15 +40,15 @@ class Pluf_Test_Fixture if (false === ($ffile=Pluf::fileExists($file))) { throw new Exception(sprintf(__('Fixture file not found: %s.'), $file)); } - $json = file_get_contents($ffile); - return self::load($json); + return self::load(file_get_contents($ffile)); } - public static function load($json) + public static function load($json, $deserialize=true) { $created = array(); - $data = json_decode($json, true); + $data = ($deserialize) ? json_decode($json, true) : $json; + unset($json); foreach ($data as $model) { if ((int)$model['pk'] > 0) { $item = new $model['model']($model['pk']); @@ -58,22 +58,34 @@ class Pluf_Test_Fixture } $m = new $model['model'](); $m->setFromFormData($model['fields']); - $m->create(true); // we force the id + $m->create(true); // we load in raw mode $created[] = array($model['model'], $model['pk']); } return $created; } - public static function dump($model) + /** + * Given a model or model name, dump the content. + * + * If the object is given, only this single object is dumped else + * the complete table. + * + * @param mixed Model object or model name + * @param bool Serialize as JSON (true) + * @return mixed Array or JSON string + */ + public static function dump($model, $serialize=true) { if (is_object($model)) { - return json_encode(array(self::prepare($model))); + return ($serialize) ? + json_encode(array(self::prepare($model))) : + array(self::prepare($model)); } $out = array(); - foreach (Pluf::factory($model)->getList() as $item) { + foreach (Pluf::factory($model)->getList(array('order' =>'id ASC')) as $item) { $out[] = self::prepare($item); } - return json_encode($out); + return ($serialize) ? json_encode($out) : $out; } /**