Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP decouple MySql queries from classes

It seems all my classes I create are filled with methods that contain MySql queries. I decided to take a shot at decoupling. Below I have my base class Customer, and my repository class CustomerRepository that's passed to the constructor if needed. The methods are basic, the save method in customer calls the create method in CustomerRepository for example. The customer class is now a little more readable but at what cost? I coded an entire other class just to do the MySql query that I could have put in the create method in the Customer class to begin with. I'm struggling to find a real world example of decoupling that will work in this scenario as it pertains to a work project. The examples I've found such as here, Proper Repository Pattern Design in PHP? (although fantastic), seem to be overly complex. My question(s) is: am I decoupling correctly? and is decoupling even necessary when the real world requires quick and somewhat dirty code to achieve the business goal as quickly as possible?

<?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();

    }

}
like image 736
Naterade Avatar asked Jun 17 '26 05:06

Naterade


1 Answers

As everyone already suggested here you need to implement ORM. See this question to choose: Good PHP ORM Library?

If you still don't want to use ORM you will need to implement the same thing by itself, this will take more time than using ready-to-go ORM. You could implement Unit of work (http://martinfowler.com/eaaCatalog/unitOfWork.html ) and Repository (http://martinfowler.com/eaaCatalog/repository.html) patterns to build you own ORM.

like image 119
Dzianis Yafimau Avatar answered Jun 19 '26 20:06

Dzianis Yafimau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!