| src/Pluf/DB.php |
| 147 | 147 | */ |
| 148 | 148 | function Pluf_DB_IdentityToDb($val, $db) |
| 149 | 149 | { |
| 150 | | if (is_null($val)) { |
| 150 | if (null === $val) { |
| 151 | 151 | return 'NULL'; |
| 152 | 152 | } |
| 153 | 153 | return $db->esc($val); |
| ... | ... | |
| 163 | 163 | |
| 164 | 164 | function Pluf_DB_SerializedToDb($val, $db) |
| 165 | 165 | { |
| 166 | | if (is_null($val)) { |
| 166 | if (null === $val) { |
| 167 | 167 | return 'NULL'; |
| 168 | 168 | } |
| 169 | 169 | return $db->esc(serialize($val)); |
| ... | ... | |
| 176 | 176 | |
| 177 | 177 | function Pluf_DB_CompressedToDb($val, $db) |
| 178 | 178 | { |
| 179 | | return (is_null($val)) ? 'NULL' : $db->esc(gzdeflate($val, 9)); |
| 179 | return (null === $val) ? 'NULL' : $db->esc(gzdeflate($val, 9)); |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | function Pluf_DB_BooleanFromDb($val) { |
| ... | ... | |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | function Pluf_DB_BooleanToDb($val, $db) { |
| 190 | | if (is_null($val)) { |
| 190 | if (null === $val) { |
| 191 | 191 | return 'NULL'; |
| 192 | 192 | } |
| 193 | 193 | if ($val) { |
| ... | ... | |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | function Pluf_DB_IntegerFromDb($val) { |
| 200 | | if (is_null($val)) return null; |
| 201 | | return (int) $val; |
| 200 | return (null === $val) ? null : (int) $val; |
| 202 | 201 | } |
| 203 | 202 | |
| 204 | 203 | function Pluf_DB_IntegerToDb($val, $db) { |
| 205 | | if (is_null($val)) { |
| 206 | | return 'NULL'; |
| 207 | | } |
| 208 | | return (string)(int)$val; |
| 204 | return (null === $val) ? 'NULL' : (string)(int)$val; |
| 209 | 205 | } |
| 210 | 206 | |
| 211 | 207 | function Pluf_DB_PasswordToDb($val, $db) { |
| src/Pluf/Model.php |
| 290 | 290 | */ |
| 291 | 291 | function _getConnection() |
| 292 | 292 | { |
| 293 | static $con = null; |
| 293 | 294 | if ($this->_con !== null) { |
| 294 | 295 | return $this->_con; |
| 295 | 296 | } |
| 297 | if ($con !== null) { |
| 298 | $this->_con = $con; |
| 299 | return $this->_con; |
| 300 | } |
| 296 | 301 | $this->_con = &Pluf::db($this); |
| 302 | $con = $this->_con; |
| 297 | 303 | return $this->_con; |
| 298 | 304 | } |
| 299 | 305 | |
| ... | ... | |
| 323 | 329 | */ |
| 324 | 330 | function __get($prop) |
| 325 | 331 | { |
| 326 | | if (array_key_exists($prop, $this->_data)) return $this->_data[$prop]; |
| 327 | | else try { |
| 328 | | return $this->__call($prop, array()); |
| 329 | | } catch (Exception $e) { |
| 330 | | throw new Exception(sprintf('Cannot get property "%s".', $prop)); |
| 331 | | } |
| 332 | return (array_key_exists($prop, $this->_data)) ? |
| 333 | $this->_data[$prop] : $this->__call($prop, array()); |
| 332 | 334 | } |
| 333 | 335 | |
| 334 | 336 | /** |
| ... | ... | |
| 339 | 341 | */ |
| 340 | 342 | function __set($prop, $val) |
| 341 | 343 | { |
| 342 | | if (!is_null($val) and isset($this->_cache['fk'][$prop])) { |
| 344 | if (null !== $val and isset($this->_cache['fk'][$prop])) { |
| 343 | 345 | $this->_data[$prop] = $val->id; |
| 344 | 346 | unset($this->_cache['get_'.$prop]); |
| 345 | 347 | } else { |
| ... | ... | |
| 737 | 739 | |
| 738 | 740 | /** |
| 739 | 741 | * Create the model into the database. |
| 742 | * |
| 743 | * If raw insert is requested, the preSave/postSave methods are |
| 744 | * not called and the current id of the object is directly |
| 745 | * used. This is particularily used when doing backup/restore of |
| 746 | * data. |
| 740 | 747 | * |
| 748 | * @param bool Raw insert (false) |
| 741 | 749 | * @return bool Success |
| 742 | 750 | */ |
| 743 | | function create($force_id=false) |
| 751 | function create($raw=false) |
| 744 | 752 | { |
| 745 | | $this->preSave(true); |
| 753 | if (!$raw) { |
| 754 | $this->preSave(true); |
| 755 | } |
| 746 | 756 | $req = 'INSERT INTO '.$this->getSqlTable()."\n"; |
| 747 | 757 | $icols = array(); |
| 748 | 758 | $ivals = array(); |
| 749 | 759 | $assoc = array(); |
| 750 | 760 | foreach ($this->_a['cols'] as $col=>$val) { |
| 751 | 761 | $field = new $val['type'](); |
| 752 | | if ($col == 'id' and !$force_id) { |
| 762 | if ($col == 'id' and !$raw) { |
| 753 | 763 | continue; |
| 754 | 764 | } elseif ($field->type == 'manytomany') { |
| 755 | 765 | // If is a defined array, we need to associate. |
| 756 | | if (is_array($this->$col)) { |
| 757 | | $assoc[$val['model']] = $this->$col; |
| 766 | if (is_array($this->_data[$col])) { |
| 767 | $assoc[$val['model']] = $this->_data[$col]; |
| 758 | 768 | } |
| 759 | 769 | continue; |
| 760 | 770 | } |
| 761 | 771 | $icols[] = $this->_con->qn($col); |
| 762 | | $ivals[] = $this->_toDb($this->$col, $col); |
| 772 | $ivals[] = $this->_toDb($this->_data[$col], $col); |
| 763 | 773 | } |
| 764 | 774 | $req .= '('.implode(', ', $icols).') VALUES '; |
| 765 | 775 | $req .= '('.implode(','."\n", $ivals).')'; |
| 766 | 776 | $this->_con->execute($req); |
| 767 | | if (false === ($id=$this->_con->getLastID())) { |
| 768 | | throw new Exception($this->_con->getError()); |
| 777 | if (!$raw) { |
| 778 | if (false === ($id=$this->_con->getLastID())) { |
| 779 | throw new Exception($this->_con->getError()); |
| 780 | } |
| 781 | $this->_data['id'] = $id; |
| 769 | 782 | } |
| 770 | | $this->_data['id'] = $id; |
| 771 | 783 | foreach ($assoc as $model=>$ids) { |
| 772 | 784 | $this->batchAssoc($model, $ids); |
| 773 | 785 | } |
| 774 | | $this->postSave(true); |
| 786 | if (!$raw) { |
| 787 | $this->postSave(true); |
| 788 | } |
| 775 | 789 | return true; |
| 776 | 790 | } |
| 777 | 791 | |
| ... | ... | |
| 956 | 970 | function _fromDb($val, $col) |
| 957 | 971 | { |
| 958 | 972 | $m = $this->_con->type_cast[$this->_a['cols'][$col]['type']][0]; |
| 959 | | return $m($val); |
| 973 | return ($m == 'Pluf_DB_IdentityFromDb') ? $val : $m($val); |
| 960 | 974 | } |
| 961 | 975 | |
| 962 | 976 | /** |
| src/Pluf/Test/Fixture.php |
| 40 | 40 | if (false === ($ffile=Pluf::fileExists($file))) { |
| 41 | 41 | throw new Exception(sprintf(__('Fixture file not found: %s.'), $file)); |
| 42 | 42 | } |
| 43 | | $json = file_get_contents($ffile); |
| 44 | | return self::load($json); |
| 43 | return self::load(file_get_contents($ffile)); |
| 45 | 44 | } |
| 46 | 45 | |
| 47 | 46 | |
| 48 | | public static function load($json) |
| 47 | public static function load($json, $deserialize=true) |
| 49 | 48 | { |
| 50 | 49 | $created = array(); |
| 51 | | $data = json_decode($json, true); |
| 50 | $data = ($deserialize) ? json_decode($json, true) : $json; |
| 51 | unset($json); |
| 52 | 52 | foreach ($data as $model) { |
| 53 | 53 | if ((int)$model['pk'] > 0) { |
| 54 | 54 | $item = new $model['model']($model['pk']); |
| ... | ... | |
| 58 | 58 | } |
| 59 | 59 | $m = new $model['model'](); |
| 60 | 60 | $m->setFromFormData($model['fields']); |
| 61 | | $m->create(true); // we force the id |
| 61 | $m->create(true); // we load in raw mode |
| 62 | 62 | $created[] = array($model['model'], $model['pk']); |
| 63 | 63 | } |
| 64 | 64 | return $created; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | | public static function dump($model) |
| 67 | /** |
| 68 | * Given a model or model name, dump the content. |
| 69 | * |
| 70 | * If the object is given, only this single object is dumped else |
| 71 | * the complete table. |
| 72 | * |
| 73 | * @param mixed Model object or model name |
| 74 | * @param bool Serialize as JSON (true) |
| 75 | * @return mixed Array or JSON string |
| 76 | */ |
| 77 | public static function dump($model, $serialize=true) |
| 68 | 78 | { |
| 69 | 79 | if (is_object($model)) { |
| 70 | | return json_encode(array(self::prepare($model))); |
| 80 | return ($serialize) ? |
| 81 | json_encode(array(self::prepare($model))) : |
| 82 | array(self::prepare($model)); |
| 71 | 83 | } |
| 72 | 84 | $out = array(); |
| 73 | | foreach (Pluf::factory($model)->getList() as $item) { |
| 85 | foreach (Pluf::factory($model)->getList(array('order' =>'id ASC')) as $item) { |
| 74 | 86 | $out[] = self::prepare($item); |
| 75 | 87 | } |
| 76 | | return json_encode($out); |
| 88 | return ($serialize) ? json_encode($out) : $out; |
| 77 | 89 | } |
| 78 | 90 | |
| 79 | 91 | /** |