16 $this->_table = $table;
23 public function load($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');
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');
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');
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);
51 elseif(!$this->db->_update($this->_table,$this->_record,$this->_key))
throw new \
Exception(
'Record disappeared');
53 foreach($this->columns as $name => $def)
if(array_key_exists($def[
'column'],$this->_key)) $key[$name] = $this->_key[$def[
'column']];
57 protected function _get($key){
58 return $this->_record[$this->columns[$key][
'column']] ?? $this->columns[$key][
'defaultValue'] ??
false;
61 protected function _set($key,$value){
62 $this->_record[$this->columns[$key][
'column']] = $value;
66 if($this->_columns === null){
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];
77 return $this->_def->component(
'db');
__construct($def, $table)
load($key)
Load the record.