user10631553
user10631553

Reputation:

The PHP file does not find a class, but the class is defined

I have a php file called "purchases.controller.php" in which within a function called 'ctrCash' of the 'Purchases' class, I pass variables to a function called 'ctrNewCashPurchase' of the 'CartController' class that I have defined, but when I run the project, I get the message:

"Fatal error : Uncaught Error: Class 'CartModel' not found in ... "

If I do a var_dump inside the function ctrNewCashPurchase, I realize that I am entering that function, but it tells me that it does not recognize 'CartModel' and I do not understand why.

I share the code of the "purchases.controller.php" file:

class CartController{

    static public function ctrNewCashPurchase($datos){

        $tabla = "compras";

        $respuesta = CartModel::mdlNewCashPurchase($tabla, $datos);

        if($respuesta == "ok"){

            $tabla = "comentarios";
            ModeloUsuarios::mdlIngresoComentarios($tabla, $datos);

        }

        return $respuesta;

    }      

}

class Purchases {

    public function ctrCash (&$arrayCompleto, &$usuario, &$direccion1, &$direccion2, &$dia, &$hora, &$email, &$telefono, &$sesion){

        if(isset($usuario)){

             //Here I create an array
             for($i = 0; $i < count($arrayCompleto); $i++){

               $datos = array("idUsuario"=> $sesion,
                              "idProducto"=> $arrayCompleto[$i]["idProducto"],
                              "metodo"=> "Efectivo",
                              "email"=> $email,
                              "direccion"=> $direccion1,
                              "detalleDireccion"=> $direccion2,
                              "diaEnvio"=> $dia,
                              "horaEnvio"=> $hora,
                              "telefono"=> $telefono,
                              "pais"=> "ARG");

                }

             $respuesta = CartController::ctrNewCashPurchase($datos);

          }

     }

}

I share the code of the "purchases.model.php" file, where I define the CartModel class:

class CartModel{

    static public function mdlNewCashPurchase($tabla, $datos){

        $stmt = Conexion::conectar()->prepare("INSERT INTO $tabla (id_usuario, id_producto, metodo, email, direccion, pais, detalleDireccion, diaEnvio, horaEnvio, telefono) VALUES (:id_usuario, :id_producto, :metodo, :email, :direccion, :pais, :detalleDireccion, :diaEnvio, :horaEnvio, :telefono)");

        $stmt->bindParam(":id_usuario", $datos["idUsuario"], PDO::PARAM_INT);
        $stmt->bindParam(":id_producto", $datos["idProducto"], PDO::PARAM_INT);
        $stmt->bindParam(":metodo", $datos["metodo"], PDO::PARAM_STR);
        $stmt->bindParam(":email", $datos["email"], PDO::PARAM_STR);
        $stmt->bindParam(":direccion", $datos["direccion"], PDO::PARAM_STR);
        $stmt->bindParam(":pais", $datos["pais"], PDO::PARAM_STR);
        $stmt->bindParam(":detalleDireccion", $datos["detalleDireccion"], PDO::PARAM_STR);
        $stmt->bindParam(":diaEnvio", $datos["diaEnvio"], PDO::PARAM_STR);
        $stmt->bindParam(":horaEnvio", $datos["horaEnvio"], PDO::PARAM_STR);
        $stmt->bindParam(":telefono", $datos["telefono"], PDO::PARAM_INT);

        if($stmt->execute()){ 

            return "ok"; 

        }else{ 

            return "error"; 

        }

        $stmt->close();

        $tmt =null;
    }

}

And I add this other file called 'aux.php' in case it influences something in the error that causes me. Here is how to send 'purchases.controller.php' parameters within the 'ctrCash' function

    if(isset($_POST['usuario'])){

    require ('purchases.controller.php');

    $arrayCompleto = json_decode($_POST['arrayCompleto'], true);

$usuario = $_POST['usuario']; 
$direccion1 = $_POST['direccion1']; 
$direccion2 = $_POST['direccion2']; 
$dia = $_POST['dia']; 
$hora = $_POST['hora']; 
$email = $_POST['email']; 
$telefono = $_POST['telefono']; 
$sesion = $_POST['sesion'];


 $payments = new Purchases(); 
$payments -> ctrCash($arrayCompleto, $usuario, $direccion1, $direccion2, $dia, $hora, $email, $telefono, $sesion);

    }

Upvotes: 0

Views: 58

Answers (1)

Shim-Sao
Shim-Sao

Reputation: 2126

The error is beacause the model is not imported for the controller.

You can include it in the controller, it's the same (in this case).

if(isset($_POST['usuario'])){

require_once ('purchases.model.php');
require_once ('purchases.controller.php');
...

}

Upvotes: 1

Related Questions