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;
}
}
}