练习创建数据库类,需要一些指导 [英] Practicing Creating DB classes, need a little guidance
问题描述
因此,我的任务是学习如何使用PDO创建自己的数据库类.我对PDO和更复杂的班级开发还很陌生,因此想在获得广泛的指导之前先获得社区的一些指导.我有一个部分构建的类,但我知道必须有一种更好/更合乎逻辑的方法来实现此目的.
So I am on a mission to learn how to create DB classes of my own using PDO. I am fairly new to PDO and more complex class development and wanted to get a little guidance from the community before I get too far into it. I have a class partially built but I know there has to be a better/more logical way to do this.
我真的很希望能够有一个查询方法,这样我就可以在我的数据库类中抛出几乎所有内容.如果这是错误的想法,请告诉我原因.
I'd really like to be able to have a single query method so I can trow almost anything at it in my DB class. If this is faulty thinking please let me know why.
当前,我有一个config.php文件,其中定义了DB常量和一个称为DB的类.这是我的代码:
Currently I have a config.php file with DB constants defined and a class called DB. Here's my Code:
在index.php中:
In index.php:
require_once 'config.php';
require_once '_inc/class.db.php';
$db = new DB();
我的班级:
public $dbh;
public function __construct(){
$dbh = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD);
}//end __construct
public function build_query( $args ){
$defaults = array(
'type' => 'SELECT',
'fields' => '*',
'table' => '',
'where' => '',
'orderby' => 'id',
'order' => 'DESC'
'offset' => '0',
'limit' => ''
);
$params = array_merge( $defaults, $args );
$sql = $params['type'];
$sql .= ' '.$params['fields'].' FROM '.$params['table'];
if( $params['where'] )
$sql .= ' WHERE '.$params['where'];
$sql .= ' ORDER BY '$params['orderby'].' '.$params['order'];
if( $limit )
$sql .= ' LIMIT '.$params['offset'].', '.$params['offset'];
return $sql;
}//end build_query
public function dbq( $args ){
$sql = $this->build_query( $args );
$this->$dbh->prepare( $sql );
return $this->$dbh->execute();
我知道我在这里错过了一些东西.请按正确的方向推动我,因为我真的很想学习这点,从而成为一名更好的PHP开发人员.我确实对使用单例模式有所了解,但想从真正知道它是如何工作和如何结合在一起的人们那里获得更多信息.
I know I am missing something here. Please just push me in the right direction as I really want to learn this to become a better PHP developer. I did look a little at using singleton pattern but wanted to get a little more info from people who really know how it all works and fits together.
提前谢谢!
推荐答案
如果我没看错您的问题,那么您正在寻找一种所谓的Query-Builder.查询构建器是具有一些参数的类,可以根据这些参数返回查询.
If I read your question right, you are looking for a so called Query-Builder. A query builder is a class that has some parameters and which can return the query based on the parameters.
实际上,如果您上面的示例就是您所需要的,那么这仅需更多,也不需要更多.您只需要将其放入自己的类中,因为数据库层的其余部分还有其他事情要做.
Actually this needs nothing more and nothing less if the example you have above is what you need. You only need to put that into a class of it's own because the rest of the database layer has other things to do.
然后可以将Query对象传递到它所属的位置.通常,您在某些班级内部使用该代码.因此,您实际上并没有在询问创建一个通用的数据库神类(这有点奇怪),而您只是想将SQL查询字符串生成器包装到它自己的类中:
You can then pass the Query object to where it belongs to. Normally you use that internally in some class. So you are actually not asking about creating a general DB god class (which would be a smell btw), but you just want to wrap the SQL Query String Builder into a class of it's own:
/**
* SQL Query
*/
class SqlQuery
{
public
$type = 'SELECT',
$fields = '*',
$table,
$where,
$orderby = 'id',
$order = 'DESC',
$offset,
$limit;
/**
* @return string
*/
public function getQuery() {
$sql = sprintf('%s %s FROM %s', $this->type, $this->fields, $this->table);
$this->where
&& $sql .= sprintf(' WHERE %s', $this->where)
;
$sql .= sprintf(' ORDER BY %s %s', $this->orderby, $this->order);
$this->limit
&& $sql .= sprintf(' LIMIT %s, %s', $this->offset, $this->limit)
;
$sql .= ';'; ### not strictly necessary but nice for debugging ###
return $sql;
}
public function __toString() {
return $this->getQuery();
}
}
### Usage: ###
$query = new SqlQuery;
$query->table = 'TABLE1';
echo $query; // SELECT * FROM TABLE1 ORDER BY id DESC;
此示例实际上涵盖了您到目前为止所拥有的所有内容,但是为它提供了已定义的接口.因此,您具有默认属性值,并且具有构建功能.然后,您可以将SqlQuery
传递到需要的地方.
This example actually covers everything you have so far but gives it a defined interface. So you have the default property values and you have the build function. You can then pass that SqlQuery
around where you need it.
这篇关于练习创建数据库类,需要一些指导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!