PHP从类中分离出MySql查询

时间:2016-01-22 02:03:36

标签: php mysql design-patterns decoupling

我创建的所有类似乎都填充了包含MySql查询的方法。我决定尝试脱钩。下面我有我的基类Customer,以及我的存储库类CustomerRepository,如果需要,它会传递给构造函数。方法是基本的,客户中的save方法 例如,调用create中的CustomerRepository方法。客户类现在更具可读性,但成本是多少?我编写了一个完整的其他类来执行MySql查询,我可以将其放在create类的Customer方法中。我正在努力寻找一个真实世界的解耦例子,它将在这个场景中起作用,因为它与工作项目有关。我在这里找到的例子Proper Repository Pattern Design in 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();

    }

}

1 个答案:

答案 0 :(得分:1)

正如大家已在此处建议的那样,您需要实施ORM。请参阅此问题以选择:Good PHP ORM Library?

如果您仍然不想使用ORM,您需要自己实现相同的功能,这比使用现成的ORM需要更多的时间。您可以实施工作单元http://martinfowler.com/eaaCatalog/unitOfWork.html )和存储库http://martinfowler.com/eaaCatalog/repository.html)模式来构建您自己的ORM。