We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

PHP Fatal error: Class 'LessonRequests' not found

Hi,

Sorry to bother you again. I have a problem with a model class named LessonRequests, it gives me PHP Fatal error. The table name of the corresponding MySQL table is lesson_requests, when I execute new LessonRequests() in my controller it gives the PHP Fatal error; if I add a line require APP_PATH . "app/models/LessonRequests.php";, it works; all other model classes works without the require line except LessonRequests. More information: I have 16 model files in the APP_PATH . "app/models/" directory. Do any of you have similar problem or know what the problem might be?

Thanks in advance


update: if I change the file name and class name to LessonRequest or LessonRequestss, it loads; if I change the file name and class name to LessonRequests_ or LessonRequests_r it fails to load. However I still haven't find the solution to make LessonRequests load.


codes:

<?php

/*
file and directory structure:
app/
    ...
    controllers/
        ...
        LessonsController.php
        LessonRequestsController.php
        TestController.php
        ...
    models/
        ...
        LessonRequests.php
        ...
    ...
public/
    ...
...
*/

/*
 * (start) loader (PHP file)
 */
$loader = new Phalcon\Loader();
$loader->registerDirs(
    array(
        APP_PATH . $config->application->controllersDir,
        APP_PATH . $config->application->libraryDir,
        APP_PATH . $config->application->modelsDir, // modelsDir = app/models/
        APP_PATH . $config->application->pluginsDir,
        APP_PATH . $config->application->customhelpersDir,
        APP_PATH . $config->application->validatorsDir
    )
)->register();
/*
 * (end) loader (PHP file)
 */

/*
 * (start) class LessonRequests (PHP file)
 */
 // even I make this file as simple as one line "class LessonRequests{}", it still doesn't work.
use Phalcon\Db\RawValue;
use Phalcon\Mvc\Model\Query;

class LessonRequests extends ModelBase{
    // .......
    // I tried to remove different parts of the codes of this class, couldn't get it to work.
}
/*
 * (end) class LessonRequests (PHP file)
 */

 /*
 * (start) class LessonsController (PHP file)
 */
class LessonsController extends ControllerBase{

    public function initialize(){
        parent::initialize();
        parent::tag_title_set('lessons');
        parent::MyTags_page_heading_set('lessons');
    }

}
/*
 * (end) class LessonsController (PHP file)
 */

/*
 * (start) class LessonRequestsController (PHP file)
 */
class LessonRequestsController extends ControllerBase{

    public function initialize(){
        parent::initialize();
        parent::tag_title_set('lesson request');
        parent::MyTags_page_heading_set('lesson request');
    }
    public function registerAction(){
        $this->dispatcher->forward(array(
            'controller' => 'lessonRequests',
            'action'     => 'registerPrepare'
        ));
        return;
    }
    public function registerPrepareAction(){
        $this->MyTags->page_heading_append('register');
        $systemUser_loggedIn = $this->systemUserLibrary->systemUser_loggedIn_get();
        $tutor_id            = $this->dispatcher->getParam('tutors_id') ? intval($this->dispatcher->getParam('tutors_id')) : NULL;
        if($tutor_id){
            $this->view->setVar('tutors_id', $tutor_id);
            $this->view->pick('lessonRequests/form');
        }else{
            parent::redirect('errors/userCauseError');
            return;
        }
    }

}
/*
 * (end) class LessonRequestsController (PHP file)
 */

/*
 * (start) (action of a controller)
 */
public function testAction(){
    require APP_PATH . 'app/models/LessonRequests.php'; // with this line, both lines below work; without this line, only the "$s = new TutorsSubjects();" line works.
    $s = new TutorsSubjects(); // corresponding to MySQL table tutors_subjects
    $l = new LessonRequests(); // corresponding to MySQL table lessons_requests
}
/*
 * (end) (action of a controller)
 */


85.5k
edited Nov '15

check the speling of the namespace of LessonRequests class.

Make sure file name is exacly LessonRequests.php ( including small and big chars ).

if you still got problems, show us the decration of the class and your loader

Show us your loader and namespace of your class. Its just not registered byt loader.



25.7k
edited Nov '15

Thank you all for your help, @Izo and @Jurigag,

I also think that the loader is the no.1 suspect, but it is strange that all other model classes within the same directory have no problem. I did suspect that perhaps it is something wrong with the LessonRequests class, but it works fine after I require the PHP file. I spent about 2 hours yesterday checking and Google, still couldn't find the problem.

below are the codes:

(to make this discussion easier to read, I moved my code to the first post of this discussion)



85.5k

i actually havent work with registerDirs hopefully someone else could land you a hand.

https://stackoverflow.com/questions/23071451/class-not-found-by-phalcon-autoloader

can you try your file name to be Lesson_requests.php or something like that, its pure guessing, just so you know :-)



25.7k

I'm not sure about those. But thanks anyway :D

i actually havent work with registerDirs hopefully someone else could land you a hand.

https://stackoverflow.com/questions/23071451/class-not-found-by-phalcon-autoloader

can you try your file name to be Lesson_requests.php or something like that, its pure guessing, just so you know :-)

Why you registerDirs ? Its really not efficient, better use registerNamespaces. Use namespaces if you dont using. Show us LessonRequests too.



25.7k
Accepted
answer
edited Nov '15

Seem like the problem is solved, however I do not really understand how the "solution" solve the problem.

I tried some "not-working" tries, one of my tries is that I renamed the MySQL table lesson_requests to something else (because I thought that maybe it is my MySQL causing the problem by any reason), this didn't solve the problem, LessonRequests model class is still not loaded. Then I tried a long shot "solution" which is removing 2 controller files LessonsController and LessonRequestsController (I test the new LessonRequests(); execution at a test controller, I did not call those 2 controllers at all.), then the LessonRequests model class loaded normally, even I later put said 2 controller files back to where they were; and perhaps I only need to remove the LessonRequestsController controller file.

I still don't know how it works. If any of you can explain or give useful information, I will truly appreciate it.

Show us cod of all 3 files and you directory strctrue and your loader code.



25.7k

Thank you for your reply @Jurigag, I updated the first post of this discussion.

Show us cod of all 3 files and you directory strctrue and your loader code.