Skip to content

Latest commit

 

History

History
executable file
·
61 lines (46 loc) · 1.75 KB

7.1Model.php.md

File metadata and controls

executable file
·
61 lines (46 loc) · 1.75 KB

Implementando Controller, Model e Views para Customers

Agora precisamos criar as views, controllers e models, no caso criarei apenas para customers.

Criar Model.php

Acessar a pasta /var/www/html/app-mvc/src/Core/

E dentro dela o arquivo Model.php

Esta é a classe base dos models. Todos os models criados devem herdar dela.

Com o conteúdo abaixo:

<?php

namespace Mvc\Core;
use PDO;

class Model
{
    /**
     * @var null Database Connection
     */
    public $db = null;

    /**
     * Whenever model is created, open a database connection.
     */
    function __construct()
    {
        try {
            self::openDatabaseConnection();
        } catch (\PDOException $e) {
            exit('Database connection could not be established.');
        }
    }

    /**
     * Open the database connection with the credentials from application/config/config.php
     */
    private function openDatabaseConnection()
    {
        // set the (optional) options of the PDO connection. in this case, we set the fetch mode to
        // "objects", which means all results will be objects, like this: $result->user_name !
        // For example, fetch mode FETCH_ASSOC would return results like this: $result["user_name] !
        // @see http://www.php.net/manual/en/pdostatement.fetch.php
        $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);

        // generate a database connection, using the PDO connector
        // @see http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
		$dsn = DB_TYPE . ':host=' . DB_HOST . ';port ='. DB_PORT . ';dbname=' . DB_NAME;// . $databaseEncodingenc;
        $this->db = new PDO($dsn , DB_USER, DB_PASS, $options);

    }
}