Pluf Framework

Pluf Framework Commit Details


Date:2009-10-19 10:45:40 (15 years 2 months ago)
Author:Loic d'Anterroches
Branch:develop, master
Commit:8cf66a115318414d124656cfa654849e64a01394
Parents: f3a7bfb5fcdb39db76f1e3b234dde3bd2910d676
Message:Improved the performance of the ORM for fast backup, restore (20% faster).

Changes:

File differences

src/Pluf/DB.php
147147
148148
149149
150
150
151151
152152
153153
......
163163
164164
165165
166
166
167167
168168
169169
......
176176
177177
178178
179
179
180180
181181
182182
......
187187
188188
189189
190
190
191191
192192
193193
......
197197
198198
199199
200
201
200
202201
203202
204203
205
206
207
208
204
209205
210206
211207
*/
function Pluf_DB_IdentityToDb($val, $db)
{
if (is_null($val)) {
if (null === $val) {
return 'NULL';
}
return $db->esc($val);
function Pluf_DB_SerializedToDb($val, $db)
{
if (is_null($val)) {
if (null === $val) {
return 'NULL';
}
return $db->esc(serialize($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) {
}
function Pluf_DB_BooleanToDb($val, $db) {
if (is_null($val)) {
if (null === $val) {
return 'NULL';
}
if ($val) {
}
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) {
src/Pluf/DB/Field.php
6363
6464
6565
66
66
67
68
6769
6870
6971
{
$this->value = $value;
$this->column = $column;
$this->extra = array_merge($this->extra, $extra);
if ($extra) {
$this->extra = array_merge($this->extra, $extra);
}
}
/**
src/Pluf/Model.php
290290
291291
292292
293
293294
294295
295296
297
298
299
300
296301
302
297303
298304
299305
......
323329
324330
325331
326
327
328
329
330
331
332
333
332334
333335
334336
......
339341
340342
341343
342
344
343345
344346
345347
......
737739
738740
739741
742
743
744
745
746
740747
748
741749
742750
743
751
744752
745
753
754
755
746756
747757
748758
749759
750760
751761
752
762
753763
754764
755765
756
757
766
767
758768
759769
760770
761771
762
772
763773
764774
765775
766776
767
768
777
778
779
780
781
769782
770
771783
772784
773785
774
786
787
788
775789
776790
777791
......
956970
957971
958972
959
973
960974
961975
962976
*/
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;
}
*/
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());
}
/**
*/
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 {
/**
* 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;
}
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);
}
/**
src/Pluf/Test/Fixture.php
4040
4141
4242
43
44
43
4544
4645
4746
48
47
4948
5049
51
50
51
5252
5353
5454
......
5858
5959
6060
61
61
6262
6363
6464
6565
6666
67
67
68
69
70
71
72
73
74
75
76
77
6878
6979
70
80
81
82
7183
7284
73
85
7486
7587
76
88
7789
7890
7991
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']);
}
$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;
}
/**

Archive Download the corresponding diff file

Branches

Tags

Number of commits:
Page rendered in 0.06809s using 13 queries.