R
R
rdt092015-11-27 12:48:26
PHP
rdt09, 2015-11-27 12:48:26

Am I implementing the Active Record pattern correctly?

I took it for educational purposes to write a simple implementation of the Active Record pattern, I got such a service . Actually the question is simple - am I doing everything right?
So here's what I did.
Working with the database is encapsulated by the DB singleton class. It connects to the database:

private function __construct() {
    $this->connect = new mysqli(self::HN, self::UN, self::PW, self::DB);
    $this->connect->query('SET NAMES utf8');
    mb_internal_encoding("UTF-8");
    if ($this->connect->connect_error) {
        die($conn->connect_error);
    }
}

and returns a connection object:
public static function getConnection() {
    if (empty(static::$instance)) {
        static::$instance = new static();
    }
    return static::$instance->connect;
}

The data of a particular book is maintained by the Book class. It returns the properties needed to display the characteristics of the book, and also updates them by calling the BookManager class to do this:
public function update(array $data) {
    $result = BookManager::update($this->id, $data);
    $this->author = $result['author'];
    $this->title = $result['title'];
    $this->pages = $result['pages'];
    $this->price = $result['price'];
}

The BookManager class actually implements Active Record. It stores the queries necessary to work with the database:
private static $selectString = "SELECT * FROM books WHERE id = ?";
private static $multipleSelectString = "SELECT * FROM books ORDER BY id LIMIT ?, ?";
private static $updateString = "UPDATE books SET author = ?, title = ?, pages = ?, price = ? WHERE id = ?";
private static $insertString = "INSERT INTO books (author, title, pages, price) VALUES (?, ?, ?, ?)";
private static $countString = "SELECT COUNT(*) FROM books";

And methods for working with these requests. The getBook method finds the book data by the given id and returns the corresponding Book class object:
static public function getBook($id) {
    $resultSet = static::getData($id);

    return new Book(
        $resultSet['id'],
        $resultSet['author'],
        $resultSet['title'],
        $resultSet['pages'],
        $resultSet['price']
    );
}

static private function getData($id) {
    global $db;
    $stmt = $db->prepare(self::$selectString);

    $stmt->bind_param('i', $id);

    $stmt->execute();
    $result = $stmt->get_result();

    $resultSet = $result->fetch_assoc();

    return $resultSet;
}

The getCount method returns the total number of books in the table:
static public function getCount() {
    global $db;

    $stmt = $db->prepare(self::$countString);
    $stmt->execute();

    $stmt->bind_result($count);
    $stmt->fetch();

    return $count;
}

The update method updates the data in the table:
static public function update($id, array $data) {
    global $db;
    $stmt = $db->prepare(self::$updateString);

    $stmt->bind_param('ssiii',
        $data['author'],
        $data['title'],
        $data['pages'],
        $data['price'],
        $id
    );

    $stmt->execute();

    $resultSet = static::getData($id);
    return $resultSet;
}

Well, and so on.
Thanks to everyone who will respond.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2015-11-27
@SilenceOfWinter

static is not OOP, global is not used for a long time. static

each instance of this class corresponds to one table entry;
wiki/ActiveRecord
Clearly out of place.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question