FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Record.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\Def;
4 
5 class Record extends \Rsi\Thing{
6 
7  protected $_def = null;
8  protected $_table = null;
9 
10  protected $_columns = null;
11  protected $_key = null;
12  protected $_record = [];
13 
14  public function __construct($def,$table){
15  $this->_def = $def;
16  $this->_table = $table;
17  }
18  /**
19  * Load the record.
20  * @param mixed $key Unique identifier for this record, based on the primary key (array when multiple columns).
21  * @return bool True when found.
22  */
23  public function load($key){
24  $this->_key = [];
25  if(!is_array($id = $key)) switch(count($key = $this->_def->key($this->_table))){
26  case 0: throw new \Exception('No primary key and no column specified');
27  case 1: $this->_key[array_pop($key)] = $id; break;
28  default: throw new \Exception('Multi column primary key and only one value given');
29  }
30  else{
31  foreach($this->columns as $name => $column) if(array_key_exists($name,$key)) $this->_key[$column['column']] = $key[$name];
32  if(!$this->_key) throw new \Exception('Empty key');
33  }
34  switch(count($records = $this->db->_select($this->_table,'*',$this->_key,null,2) ?: [])){
35  case 0: return $this->_key = false;
36  case 1: $this->_record = array_pop($records); break;
37  default: throw new \Exception('Key not unique');
38  }
39  return true;
40  }
41  /**
42  * Save the record.
43  * @return mixed Key on success (auto insert ID when applicable; array when multiple columns; false when no primary key).
44  */
45  public function save(){
46  if(!$this->_key){
47  $this->db->_insert($this->_table,$this->_record);
48  if(!($key = $this->_def->key($this->_table))) return false;
49  $this->_key = ($id = $this->db->lastInsertId()) ? [array_shift($key) => $id] : \Rsi\Record::select($this->_record,$key);
50  }
51  elseif(!$this->db->_update($this->_table,$this->_record,$this->_key)) throw new \Exception('Record disappeared');
52  $key = [];
53  foreach($this->columns as $name => $def) if(array_key_exists($def['column'],$this->_key)) $key[$name] = $this->_key[$def['column']];
54  return $key;
55  }
56 
57  protected function _get($key){
58  return $this->_record[$this->columns[$key]['column']] ?? $this->columns[$key]['defaultValue'] ?? false;
59  }
60 
61  protected function _set($key,$value){
62  $this->_record[$this->columns[$key]['column']] = $value;
63  }
64 
65  protected function getColumns(){
66  if($this->_columns === null){
67  $this->_columns = [];
68  foreach($this->_def->table($this->_table) as $column => $def){
69  $def = $this->_def->column($this->_table,$column);
70  $this->_columns[$def['alias'] ?? $column] = $def + ['column' => $column];
71  }
72  }
73  return $this->_columns;
74  }
75 
76  protected function getDb(){
77  return $this->_def->component('db');
78  }
79 
80 }
Basic object.
Definition: Thing.php:13
save()
Save the record.
Definition: Record.php:45
_set($key, $value)
Definition: Record.php:61
__construct($def, $table)
Definition: Record.php:14
load($key)
Load the record.
Definition: Record.php:23