The standard MVC Model class in PHP

Asked

Viewed 1,324 times

1

I am studying MVC, I created the abstract Model class to serve as a "template" for the others who can inherit it. I would like an evaluation of the code as, positive and negative points, how best to improve it, etc.

abstract class Model {

private $sql;

protected function setSql($sql_query) {

    return isset($sql_query) ? $this -> sql = $sql_query : false;
}

public function getAll() {

    $query = $this -> db -> prepare($this -> sql);
    try {
        $query -> execute();
        return $query -> fetchAll(PDO::FETCH_ASSOC);

    } catch(PDOException $e) {
        die($e -> getMessage());
    }
}

public function getById($id) {

    $query = $this -> db -> prepare($this -> sql);
    $query -> bindValue(1, $id);

    try {
        $query -> execute();
        return $query -> fetch(PDO::FETCH_ASSOC);

    } catch(PDOException $e) {
        die($e -> getMessage());
    }
}

public function getBySearch($search_term) {

    $query = $this -> db -> prepare($this -> sql);
    $query -> bindValue(1, $search_term);

    try {
        $query -> execute();
        return $query -> fetchAll(PDO::FETCH_ASSOC);

    } catch(PDOException $e) {
        die($e -> getMessage());

    }

}

public function deleteById($id) {/*será implementada*/}
public function updateById($id) {/*será implementada*/}
public function insert() {/*será implementada*/ }

}

And this would be the call in a daughter class:

public function getUserById($id) {

    $this -> setSql("SELECT * FROM XXX WHERE id = ?");
    return $this -> getById($id);

  • This question is being discussed from the Meta: http://meta.pt.stackoverflow.com/questions/1510/devemos-aceitar-pedidos-improvementof c%C3%B3digo

  • I recommend using Doctrine. It’s an excellent tool for abstraction and relationship mapping. Check out the documentation

1 answer

3

Positives

  • Your Model class is very reminiscent of the DAO (Data Access Object) classes, which separates the responsibility of rescuing objects (results) from the responsibility of representing the object.

Negative Points

  • It would be nice to abstract the result (each row of the database) instead of returning an associative array

  • You are using SQL, if you create an Object to Represent the query criterion you will get an SQL and Nosql database independence.

Example

I believe that everything should be abstracted, to make development more natural, for example instead of:

echo $model['first_name'].' '.$model['last_name']

you could type

echo $model->full_name;

The function would take care of merging the data into more useful information.

The same thing applies to the query, instead of

$person = $personModel->getBySearch("SELECT * FROM person WHERE name = 'Joe'");

You could use

$person = Person::$objects->filter(array('name'=>'Joe'))->first();

Or else

$person = Person::$objects->byName('Joe')->first();

In Rails, for example, it creates methods to filter each attribute (automatically)

  • Thanks for the tips, I’m a little new in this, I understood and did not understand at the same time rs, which design patterns I should study?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.