What is the difference between the design patterns Data Mapper and Active Record?

11

I would like to know the main differences between these two design patterns.

I have this doubt because to know the news of CakePHP 3.0 I saw the change of the design pattern used by the framework.

Currently in version 2.x Active Record is used and the Data Mapper will now be used.

Bonus: How will this affect the model layer modeling?

    
asked by anonymous 11.07.2014 / 17:18

1 answer

12

Both standards address how you control your persistence layer in an object-oriented project on a relational basis. Both standards have been proposed by Martin Fowler in the book "Patterns of Enterprise Application Architecture". Trying to explain the patterns:

Active Record

In Active Record the object has its basic CRUD operations on itself. This way, it is easier to consult. Exemplifying in PHP would look something like this:

class Customer 
{
    // Necessário injetar a base aqui
    protected $db;

    // Propriedades do objeto (colunas)
    public $id;
    public $name;

    public function __construct(PDO $db) {
        $this->db = $db;
    }

    public function do_something() {
        $this->bar .= uniqid();
    }

    public function save() {
        if ($this->id) {
            $sql = "UPDATE customer SET name = :bar WHERE id = :id";
            $statement = $this->db->prepare($sql);
            $statement->bindParam("name", $this->name);
            $statement->bindParam("id", $this->id);
            $statement->execute();
        }
        else {
            $sql = "INSERT INTO customer (name) VALUES (:name)";
            $statement = $this->db->prepare($sql);
            $statement->bindParam("bar", $this->name);
            $statement->execute();
            $this->id = $this->db->lastInsertId();
        }
    }
}

In my business class, to manipulate this object, it's simple, just call its methods, without delegating to a third class . For example.

class BusinessLogic {
    public function saveNewCustomer() {
        $customer = new Customer($db);
        $customer->name = 'Meu Cliente';
        $customer->save();
    }
}

Data Mapper

In the Data Mapper, your class will not have any methods related to CRUD or its operations. There will only be possible getters and setters of the attributes (depending on the language) and some methods like clone() , toString() and others, if necessary. That way this object gets "cleaner."

So, to work with an object in the Data Mapper pattern, you must pass the object to a third class that will perform the implemented operations (usually a DAO).

Let's exemplify:

// Classe só com os atributos
class Customer  {
    public $id;
    public $name;
}

And then a DAO that will perform the operations on top of the object Customer .

class CustomerDAO {

    // Necessário injetar o banco ou conexão aqui
    protected $db;

    public function __construct(PDO $db) {
        $this->db = $db;
    }

    public function saveCustomer(Customer &$customer) {
        if ($foo->id) {
            $sql = "UPDATE customer SET name = :name WHERE id = :id";
            $statement = $this->db->prepare($sql);
            $statement->bindParam("name", $foo->name);
            $statement->bindParam("id", $foo->id);
            $statement->execute();
        }
        else {
            $sql = "INSERT INTO customer (name) VALUES (:name)";
            $statement = $this->db->prepare($sql);
            $statement->bindParam("name", $foo->name);
            $statement->execute();
            $foo->id = $this->db->lastInsertId();
        }
    }
}

And in my business class:

class BusinessLogic {
    public function saveNewCustomer() {
        $customer = new Customer();
        $customer->name = 'Meu Cliente';
        $dao = new CustomerDAO($db);
        $dao->saveCustomer($customer);
    }
}

Finishing

Well, I'm not going to say which is better or worse. Both standards have their advantages and disadvantages. I've worked with both. In Java with Data Mappers and in Ruby on Rails with Active Record. Test both. There are several frameworks in several languages. See some in PHP:

PHP - Active Record

Most PHP frameworks implement their own Active Record solution. CakePHP as well. Besides Cake there are a few others:

Take a look, if you can, at Active Ruby on Rails Record .

PHP - Data Mapper

16.07.2014 / 05:17