Mam do zrobienia projekt na zaliczenie z baz danych, muszę zaprojektować bazę danych np sklepu (co już zrobiłem) oraz przedstawienie systemu w php przy użyciu pdo jako powłoki dostępu do bazy oraz wzorzec programowy dao.
Poniżej przedstawię kawałek kodu na którym mam się oprzeć w projekcie, puźniejwyjaśnię o co chodzi:
klasa DaoTemplate do operacji na bazie
CODE
class DaoTemplate extends PDO {
protected $connection;
protected $logger;
protected $atributetypes = array ();
protected $selectsql;
protected $insertsql;
protected $updatesql;
protected $deletesql;
public function getKey() {
return 'id';
}
public function __construct($connection = null){
$this->connection = $DaoPdoConnection;
}
public function insert($data){
$sql = $this->bind($this->insertsql, $data);
return $this->query($sql);
}
public function update($data){
$sql = $this->bind($this->updatesql, $data);
return $this->query($sql);
}
public function delete($data){
$sql = $this->bind($this->deletesql, array($this->getKey() => $id));
return $this->query($sql);
}
public function select($data){
$sql = $this->bind($this->selectsql, array($this->getKey() => $id));
$ret = $this->query($sql);
if(!$ret){
return false;
}
if(count($ret) > 0)
return $this->mapping($ret[0]);
else
return null;
}
public function mapping(array $record){
}
public function bind($rozkaz,$data){
}
?>
protected $connection;
protected $logger;
protected $atributetypes = array ();
protected $selectsql;
protected $insertsql;
protected $updatesql;
protected $deletesql;
public function getKey() {
return 'id';
}
public function __construct($connection = null){
$this->connection = $DaoPdoConnection;
}
public function insert($data){
$sql = $this->bind($this->insertsql, $data);
return $this->query($sql);
}
public function update($data){
$sql = $this->bind($this->updatesql, $data);
return $this->query($sql);
}
public function delete($data){
$sql = $this->bind($this->deletesql, array($this->getKey() => $id));
return $this->query($sql);
}
public function select($data){
$sql = $this->bind($this->selectsql, array($this->getKey() => $id));
$ret = $this->query($sql);
if(!$ret){
return false;
}
if(count($ret) > 0)
return $this->mapping($ret[0]);
else
return null;
}
public function mapping(array $record){
}
public function bind($rozkaz,$data){
}
?>
klasa DaoAdmin
CODE
<?php
class DaoAdmin extends DaoTemplate {
private static $insert = 'INSERT INTO admin (imie, nazwisko, nik, login, haslo) VALUES (:imie, :nazwisko, :nik, :login, :haslo)';
private static $update = 'UPDATE admin SET imie=:imie, nazwisko=:nazwisko, nik=:nik, login=:login, haslo=:haslo WHERE login=:login';
private static $select = 'SELECT imie, nazwisko, nik, login, haslo FROM admin where login=:login';
private static $delete = 'DELETE * FROM admin WHERE login=:login';
protected $atributetypes = array(
'imie' => PDO::PARM_STR,
'nazwisko' => PDO::PARM_STR,
'nik' => PDO::PARM_STR,
'login' => PDO::PARM_STR,
'haslo' => PDO::PARM_STR,
);
public function __construct($connection = null){
parent::__construct($connection);
}
public function mapping(array $tab){
return Admin::createObjectFromArray($tab);
}
public function save(Admin $object){
$data = $object -> toArray();
if($this->select($object->getLogin()) == null){
$ret = $this->insert($data);
}
else $ret = $this->update($data);
return $ret;
}
public function get($data){
$ret = $this->select($data);
}
public function delete($data){
$ret = $this->delete($data);
}
}
?>
klasa Admin
CODE
<?php
class Admin{
protected $id;
protected $imie;
protected $nazwisko;
protected $login;
protected $haslo;
private function setId($data){
$this->id = $data;
}
private function setLogin($data){
$this->login = $data;
}
private function setHaslo($data){
$this->haslo = $data;
}
private function setImie($data){
$this->imie = $data;
}
private function setNazwisko($data){
$this->nazwisko = $data;
}
public function getLogin(){
return $this->login;
}
public function getId(){
return $this->id;
}
public function createObjectFromArray($tab){
$admin = new Admin();
$keys = array_keys($tab);
foreach ($keys as $el){
switch ($el){
case 'id':
$admin->setId($tab[$el]);
break;
case 'imie':
$admin->setImie($tab[$el]);
break;
case 'nazwisko':
$admin->setNazwisko($tab[$el]);
break;
case 'login':
$admin->setLogin($tab[$el]);
break;
case 'haslo':
$admin->setHaslo($tab[$el]);
break;
}
}
return $admin;
}
public function toArray(){
foreach ($this as $key => $value){
$tab[$key] = $value;
}
return $tab;
}
}
?>
Mój problem polega na tym ze nie wiem jak zrobić metodę bind w DaoTemplate, mógłbym tą metodę zrobić w każdej klasie z osobna ale nie wiem czy to dobry pomysł, pozatym jesli tak miłbym robić to bezużyteczną jest tablica atrybutetypes.