我正在用PHP构建一个Web应用程序。通常,对象将与数据库中的特定行相关。我想写一些类来允许操纵那些行。这是一个例子:
编辑:我认为这总结了我尝试做的事情,只是在PHP中寻找一个实际的例子http://martinfowler.com/eaaCatalog/dataMapper.html
// Create a new company record in the database
$data = array(
'name' => 'My Test Company';
);
try {
$companyId = Company_Common::create($data);
} catch (Exception $e) {
exit('Error: ' . $e->getMessage() . "\n");
}
// Load the record from the database
try {
$company = new Company($companyId);
} catch (Exception $e) {
exit('Error: ' . $e->getMessage() . "\n");
}
// Rename the company
$company->name = 'Company Name New';
$company->save();
class Company_Common {
function create($data) {
// add code here to check required fields, insert company record into database, then return row id
}
}
class Company {
public function __construct($id) {
// add code here to load database row from company table based on id
// throw exception if id not found in database table
if (mysql_num_rows($result) < 1) { // something like this depending on database
throw new Exception('Company Not Found');
}
// dynamically set properties from database row
$row = mysql_fetch_assoc($result);
foreach($row as $key => $value) {
$this->{$key} = $value;
$this->original->{$key} = $value;
}
}
function save() {
// load another copy from database
$original = new Company($this->id);
// add code here to compare $this with $original, build update query, and update any fields that have changed
}
function delete() {
// add code here to delete company record from database
}
}
除此之外,上面的类将包含getContactIds等函数,它将根据公司ID从contacts表中检索一堆行id。
我真的只是在对其他人对这种方法的看法以及应该或可以以不同/更有效的方式做些什么的反馈之后。感谢。
答案 0 :(得分:0)
我个人更喜欢创建一个显式函数调用来从数据库中提取数据,而不是将其放在构造函数中。这允许我创建类的虚拟实例以用于测试和模拟目的。一般类可能如下所示:
class Car {
// optionally prevent cars from being instantiated any other way
private function __construct() {}
// get car from db
public static function find($id) {
// your db stuff here
$data = $db->where('id', $id)->get();
return self::make($data);
}
// mock car - create instance and return with data
public static function make(array $data) {
$instance = new self();
return $instance->fill($data);
}
// set the provided data to the model
private function fill(array $data) {
$this->make = isset($data['make']) ? $data['make'] : false;
$this->model = isset($data['model']) ? $data['model'] : false;
return $this;
}
public function save() {
// save to db
}
}
并且用法是:
// car with data from db
$car = Car::find(1);
// car with data from input
$make = $_GET['make'];
$model = $_GET['model'];
$car = Car::make(array('make' => $make, 'model' => $model));
$car->save();
// car with no data
$car = Car::make(array());
这里的主要好处是您可以为单元测试创建模拟对象,并且可以使用来自db以外的源的数据创建模型
答案 1 :(得分:0)
了解PDO!
为此我喜欢使用PDO::FETCH_CLASS
因为它允许很酷的功能。考虑这个课程:
class myClass
{private $foo,$bar;}
让我说我有一个带字段foo和bar的表,我想用这些字段实例化这个类的对象。我会这样做:
$stmt = $db->prepare( "SELECT foo,bar,'magic' as custom FROM table WHERE id=28");
$stmt->setFecthMode(PDO::FETCH_CLASS,'myClass');
$object = $stmt->fetch();
var_dump($object);
$ object现在是myClass的一个实例化对象,有三个属性,两个来自数据库,一个是我手动输入的sql
Here是使用oop析构函数
将这些对象保存回数据库的方式