Active Record Design Pattern

I am finally wrapping my head around on how to utilize PHP Object-Oriented Programming better by using the Active Record Design Pattern:

I know I have a ways to go and I promise not show you all the code when I am done. :rofl:

I just show a working Sandbox that I will be implementing for my websites.

I won’t bore you with the Database Connection:

DatabaseObject.php file:

<?php


namespace Sandbox;

use PDO;
class DatabaseObject extends Database
{
static protected string $table = "";
static protected array $columns = [];

protected static function retrieveRecords($records): array
{

    // results into objects
    $object_array = [];
    while($record = $records->fetch(PDO::FETCH_ASSOC)) {
        $object_array[] = static::instantiate($record);
    }

    return $object_array;
}

public static function fetch_all(): array
{
    $query = "SELECT * FROM " . static::$table;
    $records = static::pdo()->query($query);
    return static::retrieveRecords($records);
}


protected static function instantiate($record): DatabaseObject
{
    $object = new static;
    // Could manually assign values to properties
    // but automatically assignment is easier and re-usable
    foreach($record as $property => $value) {
        if(property_exists($object, $property)) {
            $object->$property = $value;
        }
    }
    return $object;
}
}

and my child class I call CMS.php

<?php


namespace Sandbox;


class CMS extends DatabaseObject
{
    protected static string $table = "cms";
    protected static array $columns = ['id', 'user_id', 'author', 'heading', 'content', 'date_updated', 'date_added'];
    public $id;
    public $user_id;
    public $author;
    public $heading;
    public $content;
    public $date_updated;
    public $date_added;



    public function __construct($args = [])
    {
        $this->user_id = $args['user_id'] ?? null;
        $this->author = $args['author'] ?? null;
        $this->heading = $args['heading'] ?? null;
        $this->content = $args['content'] ?? null;
        $this->date_updated = $args['date_updated'] ?? null;
        $this->date_added = $args['date_added'] ?? null;
    }

}

What’s taking me a little longer is the tutorial is using mysqli which I particularly care for, so I’m converting it over to PDO. Though I’m not a big fan of static variable and static methods, but I’m starting to understand them and once I get a better knowledge have a better understanding when to use and not to use them. Learning this and grid/flex for CSS in hopes in making my websites better without having to use frameworks which I’m know a big fan of as I like developing my own classes and designing my own HTML/CSS. The $columns variable isn’t really need right as that is use when I actually start my CRUD implementation and I believe the __construct is needed ether, but I haven’t test that yet.

I sure don’t know why the person teaching this tutorial doesn’t use PHP PDO as it is much simpler to use.

class DatabaseObject extends Database
{
static protected string $table = “”;
static protected array $columns = [];

public static function fetch_all(): array
{
    $query = "SELECT * FROM " . static::$table;
    $stmt = static::pdo()->query($query);
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

public static function fetch_by_id($id): array
{
    $query = "SELECT " . implode(", ", static::$columns) . " FROM " . static::$table . " WHERE id=:id LIMIT 1";
    $stmt = static::pdo()->prepare($query);
    $stmt->execute(['id' => $id]);
    return $stmt->fetch(PDO::FETCH_ASSOC);
}

}

Sponsor our Newsletter | Privacy Policy | Terms of Service