DAOパターンを正しく使用しているかどうか、さらに具体的には抽象的なデータベースの永続性がマッパークラスに到達するまでにどのようになるかを調べようとしています。データアクセス抽象化オブジェクトとしてPDOを使用していますが、クエリをあまり抽象化しようとしているのではないかと思うことがあります。PHPデータアクセスオブジェクト
私はどのように選択クエリを抽象化しているのかを説明しましたが、すべての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の正しい使用である、または私はそれが目的だ誤解のですか?
この程度のクエリプロセスを抽象化しているのは不必要なことですか、まれにしかありませんか?私は物事があまりにも簡単にしようとしているように時々私はそれは必ずしも必要ではないが、それは確かに一般的な方法です
したがって、ドメインモデルの各ドメインオブジェクトは、CRUD操作のために関連するDaoオブジェクトを持つことができますか? – jerry
@saddog - 各ルートレベルオブジェクト、はい。例えば、 'Order'とその' LineItem'子のリストは、おそらく 'OrderDao'を通して一緒に格納されます。残念ながら、ドメイン・モデル・オブジェクトを永続化する方法からビジネス・ロジックを完全に分離することはめったにありません。 –
さて、実際にはもう1つ質問があります。それから私は受け取り、あなたに恩恵を与えます。 LineItemの子供について言及すると、私は自分のアプリケーションでこれらを正しく処理していることを確認したいと思います。これらの子は、db(LineItemテーブル)のテーブルにマップされ、DaoによってOrderドメインオブジェクトに統合されるだけですか?つまり、DaoUser内のcreateObjectメソッドは、これらの2つのオブジェクトを一緒にするものでしょうか? – jerry