似乎我创建的所有类都充满了包含MySql查询的方法。我决定尝试脱钩。下面我有我的基类Customer
,以及我的存储库类CustomerRepository
,如果需要,它会传递给构造函数。方法是基本的,客户save
的方法例如,在CustomerRepository
中调用 create
方法。客户类现在更具可读性,但代价是什么?我编写了一个完整的其他类,只是为了执行 MySql 查询,我可以先将其放入Customer
类的 create
方法中。我正在努力寻找一个真实的解耦示例,该示例将在这种情况下起作用,因为它与工作项目有关。我找到的例子,比如这里,PHP中的正确存储库模式设计?(虽然很棒),似乎过于复杂。我的问题是:我是否正确解耦?当现实世界需要快速且有些肮脏的代码来尽快实现业务目标时,解耦是否有必要?
<?php
/*
CRUD
create, read, update, delete
*/
class Customer {
public $CustomerRepository;
public $id;
public $first_name;
public $last_name
public $email;
public $phone;
public function __construct( CustomerRepository $CustomerRepository = null ) {
if( !is_null( $CustomerRepository ) {
$this->CustomerRepository = $CustomerRepository;
}
}
public function save() {
return $this->CustomerRepository->create(
$this->first_name,
$this->last_name,
$this->email,
$this->phone
);
}
public function find() {
return $this->CustomerRepository->read( $this->id );
}
public function edit() {
return $this->CustomerRepository->update(
$this->first_name,
$this->last_name,
$this->email,
$this->phone,
$this->id
);
}
public function remove() {
return $this->CustomerRepostitory->delete( $this->id );
}
public function populate( $id ) {
$customer = $this->find( $id );
$this->id = $customer['id'];
$this->first_name = $customer['first_name'];
$this->last_name = $customer['last_name'];
$this->email = $customer['email'];
$this->phone = $customer['phone'];
}
}
class CustomerRepository {
private $Database;
public function __construct() {
if( is_null( $this->Database ) {
$this->Database = new Database();
}
}
public function create( $first_name, $last_name, $email, $phone ) {
$this->Database->query( 'INSERT INTO customers( first_name, last_name, email, phone )
VALUES( :first_name, :last_name, :email, :phone )' );
$this->Database->bind( ':first_name', $first_name );
$this->Database->bind( ':last_name', $last_name );
$this->Database->bind( ':email', $email );
$this->Database->bind( ':phone', $phone );
return $this->Database->getLastID();
}
public function read( $id ) {
$this->Database->query( 'SELECT * FROM customers WHERE id = :id LIMIT 1' );
$this->Database->bind( ':id', $id ):
return $this->Database->single();
}
public function update( $first_name, $last_name, $email, $phone, $id ) {
$this->Database->query( 'UPDATE customer SET
first_name = :first_name,
last_name = :last_name,
email = :email,
phone = :phone WHERE id = :id' );
$this->Database->bind( ':first_name', $first_name );
$this->Database->bind( ':last_name', $last_name );
$this->Database->bind( ':email', $email );
$this->Database->bind( ':phone', $phone );
$this->Database->bind( ':id', $id );
return $this->Database->execute();
}
public function delete( $id ) {
$this->Database->query( 'DELETE FROM customers WHERE id = :id LIMIT 1' );
$this->Database->bind( ':id', $id ):
return $this->Database->execute();
}
}
正如每个人都已经建议的那样,您需要实现ORM。看到这个问题来选择: 好的 PHP ORM 库?
如果您仍然不想使用 ORM,则需要自行实现相同的东西,这将比使用现成的 ORM 花费更多时间。您可以实现工作单元 (http://martinfowler.com/eaaCatalog/unitOfWork.html)和存储库(http://martinfowler.com/eaaCatalog/repository.html)模式来构建您自己的ORM。