-1

I am trying to display the database on the database.phtml page of my website. This page is a view.

So far I have a model called DatabaseConect.php

<?php

abstract class DatabaseConect {

protected $db = NULL;

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

class Database extends DatabaseConect {

public $props = array ();

private function getPage ($id) {
    $q = $this->db->prepare('SELECT * FROM retrofootball_products');
    $ret = $res->fetchAll();
    return ($this->props=$ret);
}

}

$db = new PDO('mysql:host=helios.csesalford.com;dbname=pd12', 'helloworld', 'password'); //I have changed the log in details 

Then in my view

<?php require('template/header.phtml') ?>

<?php 
$page = new Database ($db);
?>

<?php require('template/footer.phtml') ?>

I have been trying to find stuff on Stackoverflow and Have come across these articles but they are little above my head as I am new to this:

Properly calling the database from Model in an MVC application? http://programmers.stackexchange.com/questions/178831/using-pdo-with-mvc

My question is what is the best way using MVC to connect to a database and then display this in the view? Do I need to use PDO::FETCHin the model to display the results into a variable and then call this variable in the view?

EDIT: Like suggest I have used a bootstrap in a controller. Do i need to create a new instance of this in my view for it to work? Also where am i running the query in the correct place?

<?php

class Dependency_Manager {

private $db;

public function __construct($settings) {
    $this->db = new PDO('mysql:host=helios.csesalford.com;dbname=helloworld', 'password',   'php54');
}

public function getDB() {
    return $db;
    }
}

class CMS {
public function __construct(PDO $db) {
    //$stmt = $db->query('SELECT * FROM retrofootball_products');
    }
}

$settings = array();

$dm = new Dependency_Manager($settings);
$cms = new CMS($dm->getDB());    
Community
  • 1
  • 1
Joshua Hornby
  • 283
  • 2
  • 6
  • 18

1 Answers1

1

FYI: view is not a template. In properly implemented MVC, views are instances (as in - objects made from classes) that are responsible for all of the UI logic. To achieve it, they often juggle multiple templates.

The confusing part about DB connection (and interaction with any other form of storage) is that it is required only at very low-level structures - data mappers book chapter.

The connection itself should be provided to each data mapper (that requires it, because not all mapper would work with SQL database) instance through factory. Code example here - the relevant part is the StructureFactory class definition.

The factory for creating data mappers would injected in an instance, which need to use the mappers. Otherwise would be violating LoD.

The view should not have any idea about the origin of data. It would only request some information from model layer.

P.S.: this ancient answer is quite outdated. It contains my understanding of MVC more then 18 month ago. I soon will get an update. You will find this answer to be much more up-to-date.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150