Я пытаюсь выяснить, правильно ли я использую шаблон DAO и, точнее, насколько абстрактное db-упорство должно быть к тому моменту, когда оно попадает в мои классы-сопоставления. Я использую PDO как объект абстракции доступа к данным, но иногда мне интересно, пытаюсь ли я слишком сильно абстрагировать запросы.
Я только что рассказал, как я абстрагирую выбор запросов, но я написал методы для всех операций CRUD.
class DaoPDO {
function __construct() {
// connection settings
$this->db_host = '';
$this->db_user = '';
$this->db_pass = '';
$this->db_name = '';
}
function __destruct() {
// close connections when the object is destroyed
$this->dbh = null;
}
function db_connect() {
try {
/**
* connects to the database -
* the last line makes a persistent connection, which
* caches the connection instead of closing it
*/
$dbh = new PDO("mysql:host=$this->db_host;dbname=$this->db_name",
$this->db_user, $this->db_pass,
array(PDO::ATTR_PERSISTENT => true));
return $dbh;
} catch (PDOException $e) {
// eventually write this to a file
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
} // end db_connect()'
function select($table, array $columns, array $where = array(1=>1), $select_multiple = false) {
// connect to db
$dbh = $this->db_connect();
$where_columns = array();
$where_values = array();
foreach($where as $col => $val) {
$col = "$col = ?";
array_push($where_columns, $col);
array_push($where_values, $val);
}
// comma separated list
$columns = implode(",", $columns);
// does not currently support 'OR' arguments
$where_columns = implode(' AND ', $where_columns);
$stmt = $dbh->prepare("SELECT $columns
FROM $table
WHERE $where_columns");
$stmt->execute($where_values);
if (!$select_multiple) {
$result = $stmt->fetch(PDO::FETCH_OBJ);
return $result;
} else {
$results = array();
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
array_push($results, $row);
}
return $results;
}
} // end select()
} // end class
Итак, мои два вопроса:
-
Является ли это правильным использованием DAO, или я неправильно истолковываю его?
-
Является ли абстрагирование процесса запроса до такой степени ненужным или даже необычным? Иногда я чувствую, что я пытаюсь сделать вещи слишком легкими...