| 1 |
<?php |
|---|
| 2 |
function checkArray($v){ |
|---|
| 3 |
if (! is_array($v)) return array($v); |
|---|
| 4 |
return $v; |
|---|
| 5 |
} |
|---|
| 6 |
|
|---|
| 7 |
abstract class RecordProxy{ |
|---|
| 8 |
protected $record; |
|---|
| 9 |
protected $dao_selector; |
|---|
| 10 |
function __construct($id, $dao){ |
|---|
| 11 |
|
|---|
| 12 |
if (is_a($id, 'jDaoRecordBase')) $this->record=$id; |
|---|
| 13 |
else { |
|---|
| 14 |
$f=jDao::get($dao); |
|---|
| 15 |
if ($id == null) $this->record=jDao::createRecord($dao); |
|---|
| 16 |
else $this->record=$f->get($id); |
|---|
| 17 |
} |
|---|
| 18 |
$this->dao_selector=$dao; |
|---|
| 19 |
} |
|---|
| 20 |
function __get($nm){ |
|---|
| 21 |
if (isset($this->record->$nm)) { |
|---|
| 22 |
$r = $this->record->$nm; |
|---|
| 23 |
return $r; |
|---|
| 24 |
} |
|---|
| 25 |
} |
|---|
| 26 |
function __set($nm, $val){ |
|---|
| 27 |
$this->record->$nm=$val; |
|---|
| 28 |
} |
|---|
| 29 |
function __call(string $name, array $arguments){ return $this->record->$name($arguments);} |
|---|
| 30 |
function insert(){ |
|---|
| 31 |
$f=jDao::get($this->dao_selector); |
|---|
| 32 |
$f->insert($this->record); |
|---|
| 33 |
$conn = jDb::getConnection(); |
|---|
| 34 |
return $conn->lastInsertId(); |
|---|
| 35 |
} |
|---|
| 36 |
function delete(){ |
|---|
| 37 |
$f=jDao::get($this->dao_selector); |
|---|
| 38 |
$f->delete($this->record->getPk()); |
|---|
| 39 |
} |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
class MapDao { |
|---|
| 43 |
private $pkey; |
|---|
| 44 |
private $map; |
|---|
| 45 |
private $map_dao; |
|---|
| 46 |
private $pkey_name; |
|---|
| 47 |
function __construct($pkey, $pkey_name, $map_dao){ |
|---|
| 48 |
$this->pkey=checkArray($pkey); |
|---|
| 49 |
$this->map_dao=$map_dao; |
|---|
| 50 |
$this->pkey_name=checkArray($pkey_name); |
|---|
| 51 |
} |
|---|
| 52 |
private function fetchAll($order=array()){ |
|---|
| 53 |
$fact = jDao::get($this->map_dao); |
|---|
| 54 |
$conditions = jDao::createConditions(); |
|---|
| 55 |
for ($i = 0; $i < count($this->pkey) ; $i++) |
|---|
| 56 |
$conditions->addCondition($this->pkey_name[$i],'=',$this->pkey[$i]); |
|---|
| 57 |
foreach ($order as $field =>$way) |
|---|
| 58 |
$conditions->addItemOrder($field, $way); |
|---|
| 59 |
return $fact->findBy($conditions)->fetchAll(); |
|---|
| 60 |
} |
|---|
| 61 |
function count(){return count($this->fetchAll());} |
|---|
| 62 |
|
|---|
| 63 |
function get($order = array()){return $this->fetchAll($order);} |
|---|
| 64 |
|
|---|
| 65 |
function select($field_names, $field_values, $order=array()){ |
|---|
| 66 |
return $this->selectBase($field_names, $field_values, $order)->fetchAll(); |
|---|
| 67 |
} |
|---|
| 68 |
function includes($field_names, $field_values){ |
|---|
| 69 |
return $this->selectBase($field_names, $field_values)->rowCount() >0; |
|---|
| 70 |
} |
|---|
| 71 |
function map($field_names, $field_values){ |
|---|
| 72 |
|
|---|
| 73 |
//1 check for unicity |
|---|
| 74 |
if (! $this->includes($field_names,$field_values)) |
|---|
| 75 |
{ |
|---|
| 76 |
|
|---|
| 77 |
$record = jDao::createRecord($this->map_dao); |
|---|
| 78 |
$pk = array_combine($this->pkey_name, $this->pkey); |
|---|
| 79 |
$additional = array_combine(checkArray($field_names),checkArray($field_values)); |
|---|
| 80 |
foreach (array_merge($pk, $additional) as $name=>$val) $record->$name=$val; |
|---|
| 81 |
|
|---|
| 82 |
$fact = jDao::get($this->map_dao); |
|---|
| 83 |
return $fact->insert($record); |
|---|
| 84 |
} |
|---|
| 85 |
return null; |
|---|
| 86 |
} |
|---|
| 87 |
function unMap($field_names, $field_values){ |
|---|
| 88 |
$properties= jDao::createRecord($this->map_dao)->getProperties(); |
|---|
| 89 |
$pk = array_combine($this->pkey_name, $this->pkey); |
|---|
| 90 |
$additional = array_combine(checkArray($field_names),checkArray($field_values)); |
|---|
| 91 |
|
|---|
| 92 |
$a=array_merge($pk, $additional); |
|---|
| 93 |
$pk=array(); |
|---|
| 94 |
$fact = jDao::get($this->map_dao); |
|---|
| 95 |
foreach ($properties as $field){ |
|---|
| 96 |
if ($field['isPK']) $pk [$field['name']] = $a [$field['name']]; |
|---|
| 97 |
} |
|---|
| 98 |
$fact->delete($pk); |
|---|
| 99 |
} |
|---|
| 100 |
function unMapAll(){ |
|---|
| 101 |
|
|---|
| 102 |
$fact = jDao::get($this->map_dao); |
|---|
| 103 |
foreach ($this->fetchAll() as $r) |
|---|
| 104 |
$fact->delete($r->getPk()); |
|---|
| 105 |
} |
|---|
| 106 |
private function selectBase($field_names, $field_values, $order=array()){ |
|---|
| 107 |
$fact = jDao::get($this->map_dao); |
|---|
| 108 |
$conditions = jDao::createConditions(); |
|---|
| 109 |
for ($i = 0; $i < count($this->pkey) ; $i++) |
|---|
| 110 |
$conditions->addCondition($this->pkey_name[$i],'=',$this->pkey[$i]); |
|---|
| 111 |
if (count(checkArray($field_names)) ==0) $fields=array(); |
|---|
| 112 |
else $fields= array_combine(checkArray($field_names), checkArray($field_values)); |
|---|
| 113 |
foreach ($fields as $name=>$value) |
|---|
| 114 |
$conditions->addCondition(trim($name),'=',trim($value)); |
|---|
| 115 |
foreach ($order as $field_id=>$way) $conditions->addItemOrder($field_id, $way); |
|---|
| 116 |
return $fact->findBy($conditions); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
?> |
|---|
| 122 |
|
|---|