vendredi 17 juillet 2015

How do implement a domain object in the following project.

I have the following example in which I am using a couple of classes, to form a simple application.

The file hierarchy seems like this.

> cupid 
    - libs 
        - request
        - router 
        - database
        - view 
    - bootstrap.php 
  - index.php 

The index.php just calls the bootstrap.php which in turn contains something like this:

// bootstrap.php
namespace cupid
use request, router, database, view; 

spl_autoload_register(function($class){ /* autoload */ });

$request  = new view; 
$response = new response; 
$router   = new router; 
$database = new database; 

$router->get('/blog/{id}', function($id) use ($database, $view) {

    $article = $database->select("SELECT blog, content FROM foo WHERE id = ?",[$id]); 

    $view->layout('blogPage', ['article'=>$article]);
}); 

As you can probably tell, my problem is this line:

$article = $database->select("SELECT blog, content FROM foo WHERE id = ?", [$id]); 

Which I don't want to use, and instead try a domain object model approach.

Now, given that I will add another folder called model, with blog.php

> cupid 
    - model
       - Blog.php
    - libs 
        ...

Fill blog.php with properties mapping rows, and getter and setters ..

class Blog {

    private $id, $title, $content, $author; 

    public function getTitle(){
        return $this->title; 
    }           

    public function setTitle($title){
        $this->title = $title; 
    }

    ...
}

Now, my question is: Assuming my understand of DOM is so far correct, and that I have a CRUD app, or PDO wrapper,

"how would the blog model interact with the database and fetch the blog"..

inside my bootstrap file ?

Aucun commentaire:

Enregistrer un commentaire