pheromix
pheromix

Reputation: 19307

Class not found

I made a research but the results was not related to my problem : I have a model whose associated table contains an underscore.

<?php

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Query;

class TableClient extends Model {

    public function getSource()
    {
        return "table_client";
    }

    function lireParCritere($critere) {

        $sSQL = "
                SELECT c.table_code,s.salle_code,s.salle_lib,c.table_lib,c.table_nb_couvert,c.table_comment 
                FROM table_client as c INNER JOIN salle s ON s.salle_code = c.salle_code
                WHERE 1 = 1 ";

        if(isset($critere["table_code"]) && $critere["table_code"] != "") {
            $sSQL .= "AND c.table_code = '" . $critere["table_code"] . "' ";    
        }

        $query = new Query($sSQL,$this->getDI());

        $ret = $query->execute();

        return $ret;

    }

    function ajouter($tab) {
        $champs= "";
        $value = "";
        $separateur ="";

        foreach ($tab as $k => $v){
            $champs .= $separateur . $k;
            $value .= $separateur . "'" . $v . "'";
            $separateur = ",";
        }
        $champs = '('.$champs.')';
        $value = '('.$value.')';
        $sSQL = "
                INSERT INTO table_client $champs
                VALUES $value
                ";
        $query = new Query($sSQL,$this->getDI());
        $ret = $query->execute();
    }

    function modifier($tab) {

        $setColumns = "";
        $separateur = "";

        foreach ($tab as $k => $v){
            if ($k == 'table_code')
                continue;
            $setColumns .= $separateur . 'table_client.' . $k . " = '" . $v . "'";
            $separateur = ",";
        }

        $sSQL = "UPDATE table_client SET ".$setColumns." WHERE table_client.table_code = '".$tab['table_code']."'";

        $query = new Query($sSQL, $this->getDI());
        $ret = $query->execute();
    }

    function supprimer($tab) {
        $sSQL = "DELETE FROM table_client WHERE table_client.table_code = '".$tab['table_code']."'";
        $query = new Query($sSQL, $this->getDI());
        $ret = $query->execute();
    }
}

?>

Inside a controller I want to call a method of this model :

public function modifierAction($id){
        $this->view->action_form = '../modifierExec';
        $this->view->titre = 'Modification de table';
        $critere = array();
        $critere["table_code"] = $id;
        $this->view->data = TableClient::lireParCritere($critere); // here is the call of the model's method
        return $this->view->pick("table/table");
    }

At runtime I get a class not found error ! So how to fix it ?

Upvotes: 0

Views: 209

Answers (1)

serghei
serghei

Reputation: 3381

  1. Check class by class_exists() function
  2. Check Phalcon autoloader config. Make sure you really put in the autoloader config class/directory/namespace/
  3. You can check class availability in constroller:

    foreach (spl_autoload_functions() as $function) {
        foreach ($function as $object) {
            if (!$object instanceof \Phalcon\Loader) {
                continue;
            }
    
            var_dump($object->getNamespaces());
            var_dump($object->getCheckedPath());
            var_dump($object->getDirs());
            var_dump($object->getClasses());
            var_dump($object->getFoundPath());
        }
    }
    
    die;
    

Edit

  1. \Phalcon\Mvc\Model\Query uses PHQL, not SQL. In function lireParCritere($critere) use FROM \Fully\Qualified\Class\Name instead FROM table_client
  2. For tests use $client = new TableClient; $client->lireParCritere($critere); instead TableClient::lireParCritere($critere);

Upvotes: 2

Related Questions