How to set the db variable in ClientModel?

0

I'm crawling on MVC and I'm catching up on a 'simple' registry. By clicking on register, the errors appear:

  

Notice: Undefined variable: db in C: \ xampp \ htdocs \ cadastromvc \ App \ Models \ ClientModel.php on line 9

     

Fatal error: Call to a member function prepare () on null in C: \ xampp \ htdocs \ cadastromvc \ App \ Models \ ClientModel.php on line 9

In index.php I call config, I do not know if the error is related to the connection to the database.

Complete project, if needed: link

Model.php

<?php
    namespace Application\Models;

    class Model
    {
        protected $db;

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

ClientModel.php

<?php
    namespace Application\Models;
    use Application\Models\Model;

    class ClienteModel extends Model
    {
        // Verifica se o Usuário já existe
        public static function existeUsuario($usuario){
            $verificaUsuario = $db->prepare("SELECT 'id' FROM 'loja_clientes' WHERE 'usuario' = :usuario");
            $verificaUsuario->bindValue(':usuario', $usuario, \PDO::PARAM_STR);
            $verificaUsuario->execute();
            if($verificaUsuario->rowCount() > 0){
                return true;
            }else{
                return false;
            }
        }
        // Verifica se o CPF já existe
        public static function existeCPF($cpf){
            $verificaCPF = $db->prepare("SELECT 'id' FROM 'loja_clientes' WHERE 'cpf' = :cpf");
            $verificaCPF->bindValue(':cpf', $cpf, \PDO::PARAM_STR);
            $verificaCPF->execute();
            if($verificaCPF->rowCount() > 0){
                return true;
            }else{
                return false;
            }
        }
        // Cadastro de usuário
        public static function cadastrar($dados){
            $sql = "INSERT INTO 'loja_clientes' (nome, email, cpf, usuario, senha) 
                    VALUES (:nome, :email, :cpf, :usuario, :senha)";
            $stmt = $db->prepare($sql);
            $stmt->bindValue(":nome", $dados['nome'], PDO::PARAM_STR);
            $stmt->bindValue(":email", $dados['email'], PDO::PARAM_STR);
            $stmt->bindValue(":cpf", $dados['cpf'], PDO::PARAM_STR);
            $stmt->bindValue(":usuario", $dados['usuario'], PDO::PARAM_STR);
            $stmt->bindValue(":senha", $dados['senha'], PDO::PARAM_STR);
            $stmt->execute();
            if($stmt){
                return true;
            }else{
                return false;
            }
        }
    }
    
asked by anonymous 12.01.2017 / 20:01

1 answer

2

To access protected $db of your base class, remove static from the methods of class ClienteModel and access it with $this->db :

<?php
    namespace Application\Models;

    class Model
    {
        protected $db;

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

Do not forget to create the constructor in ClienteModel by passing the value of PDO and always access with $this->db where your code is only $db .

class ClienteModel extends Model
{
    public function __construct(\PDO $db)
    {
        parent::__construct($db);
    }

    // Verifica se o Usuário já existe
    public function existeUsuario($usuario)
    {
        $sql = "SELECT 'id' FROM 'loja_clientes' WHERE 'usuario' = :usuario";
        $verificaUsuario = $this->db->prepare($sql);
        $verificaUsuario->bindValue(':usuario', $usuario, \PDO::PARAM_STR);
        $verificaUsuario->execute();
        if($verificaUsuario->rowCount() > 0){
            return true;
        }else{
            return false;
        }
    }

Work on this case with instances:

$db = new PDO('mysql:host=127.0.0.1;port=3306;dbname=BANCO;charset=UTF8;',
        'root',
        'password');
$clienteModel = new ClienteModel($db);

This code reflects the problems encountered in your question, of course this can be further improved, but I just kept getting your problem clean.

    
12.01.2017 / 20:20