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

Models in Multiple Module

Hi.

There is big problem for beginners with that. So I'm trying to just show some variable from database. I'm basing on mutliple module from github.

Here is my Controller:

    <?php

    namespace Multiple\Frontend\Controllers;

    use Phalcon\Mvc\Controller;
    use Phalcon\Forms\Form;
    use Phalcon\Forms\Element\Text;
    use Phalcon\Forms\Element\Select;

    (....)

    public function indexAction()
    {   

        // SET META DESCRIPTIONS

        $projects = Projects::find(
            array(
                "limit" => 3
            )
        );
    }

Easy as that.

And here is my Module autoloader:

    public function registerAutoloaders()
    {

        $loader = new Loader();

        $loader->registerNamespaces(array(
            'Multiple\Frontend\Controllers' => '../apps/frontend/controllers/',
            'Multiple\Frontend\Models' => '../apps/frontend/models/',
        ));

        $loader->registerDirs(
            array(
                'formsDir' => '../apps/frontend/forms/',
                'libraryDir' => '../apps/frontend/library/'
            )
        );

        $loader->registerClasses(
            array(
                'Swift_Message' => '../apps/frontend/library/swift/swift_required.php',
                'Swift_SmtpTransport' => '../apps/frontend/library/swift/swift_required.php',
                'Swift_Mailer' => '../apps/frontend/library/swift/swift_required.php',
            )
        );

        $loader->register();

    }

Nothing fancy. Problem is that I get error: Fatal error: Class 'Multiple\Frontend\Controllers\Projects' not found, so Phalcon don't even have clue that I want to use my Model.

One big problem about Phalcon is that when amateurs who go from clear PHP to PHP Framework try to learn whole MVC thing, they don't have any examples. You'll say that we have tutorial - but tutorial is about single module app. You'll say that we have documentation - but documentation is assuming that you have rest of your project ready to go. You'll say that we have examples on github, but these examples are empty or just don't work with project what you already made with tutorials and docs.

This is big thing. I'm really not beginner in programming and documentations but I really have problems to understand PHalcon. Wherever I go project tree looks different. Try to make project with Developer Tools and it will not look like examples on github.

I would like to go with Phalcon, because performance. I would like to donate, help with translating documentation for my country, but for real, it's not opened for beginners in MVC design.

edited Mar '16

Fatal error: Class Multiple\Frontend\Controllers\Projects not found that is not a model, but a controller class that is missing... does the file even exist in apps/frontend/controllers/?

The idea behind Phalcon is to be as flexible as possible. Different examples use different folder structures, simply because the framework allows it. If you are unfamiliar with Phalcon, I'd suggest creating a simple project first, not none with modules.



9.3k
Accepted
answer
edited Mar '16

Your problem is in code

$projects = Projects::find(
        array(
            "limit" => 3
        )
    );

You are using namespaces, but there is no namespace for Projects model and PHP will try to find it under current namespace, but the file is not there.

Use \Multiple\Frontend\Models\Projects or add use Multiple\Frontend\Models\Projects and it should work.

But as Lajos said, if you are not familiar with some framework, don't try to build complicated apps, it will probably be fail anyway. Start easy with simple project, lear basics and go on.

@Lajos The only reason he's getting that error is because he's working in the namespace Multiple\Frontend\Controllers and hasn't defined this: use Multiple\Frontend\Models\Projects as Projects;

Not because it's a controller class that is missing.

@Lajos The only reason he's getting that error is because he's working in the namespace Multiple\Frontend\Controllers and hasn't defined this: use Multiple\Frontend\Models\Projects as Projects;

Not because it's a controller class that is missing.

True that! Please disregard that part of my answer



1.4k

Yea guys. Thanks. I'm really losing my mind sometimes :P When I saw Phalcon for first time I thought I will learn it in about 2-3 weeks. I did harder things in my life than that :) But naah. It's about 6 weeks and I'm still learning how to use namespaces.

Now I have some SQL error what I was getting some time ago while trying to make "simple application", but it's ok. Hardest behind me :D

Thanks again.

At first, the namespaces confused me too but they do make everything very flexible once you get the hang of them. Phalcon itself took some getting used to as well but I wouldn't trade it for anything else now that I got the basics down :)

If you want keep model definitions in modules and use models in all modules then you need to register models before registering modules or register each used model in each module. I prefer first option. I just register all models and dont care about rest.