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

Going from 2->3... Problem accessing model

Kind of modelling my latest project after Vokuro and running into an issue. When I try to do User::findFirst([...]), I'm getting a 500 error that " Class 'Zipline\Controllers\User' not found in /var/www/html/zipline/app/controllers/NoauthajaxController.php on line 11"

Here is part of my config:

    'application' => [
        'controllersDir' => APP_PATH . '/controllers/',
        'modelsDir' => APP_PATH . '/models/',
        //'formsDir' => APP_PATH . '/forms/',
        'viewsDir' => APP_PATH . '/views/',
        'libraryDir' => APP_PATH . '/library/',
        //'pluginsDir' => APP_PATH . '/plugins/',
        //'cacheDir' => BASE_PATH . '/cache/',
    ],

Here is my loader:

    <?php

    use Phalcon\Loader;

    $loader = new Loader();

    /**
     * We're a registering a set of directories taken from the configuration file
     */
    $loader->registerNamespaces([
        'Zipline\Models' => $config->application->modelsDir,
        'Zipline\Controllers' => $config->application->controllersDir,
        //'Zipline\Forms' => $config->application->formsDir,
        'Zipline' => $config->application->libraryDir
    ]);

    $loader->register();

    // Use composer autoloader to load vendor classes
    require_once BASE_PATH . '/vendor/autoload.php';

Here is my controller:

    <?php namespace Zipline\Controllers;

    use Phalcon\Http\Response;
    use Phalcon\Http\Request;

    class NoauthajaxController extends ControllerBase {

        public function userloginAction() {
            $response = $this->getResponse();
            $request = $this->getRequest();
            $U = User::findFirst([
                'conditions' => 'email = :username:',
                'bind' => [
                    'username' => $request->username
                ]
            ]);
            if (empty($U)) {
                $response['error'] = "Invalid username or password.";
            } else if ($U->login($request)) {
                $response['success'] = true;
                $response['data']['user'] = $this->session->get('auth_user');
            } else {
                $response['error'] = $this->errors($U->getMessages());
            }

            $this->respond($response);
        }
    }

Here is my model:

    <?php namespace Zipline\Models;

    use Phalcon\Mvc\Model;
    use Phalcon\Mvc\Model\Message;
    /*
    use Phalcon\Validation;
    use Phalcon\Mvc\Model\Validator\Uniqueness;
    use Phalcon\Mvc\Model\Validator\Email;
    use Phalcon\Mvc\Model\Validator\PresenceOf;
    use Phalcon\Mvc\Model\Behavior\SoftDelete;
    */

    class User extends Model {

        public function initialize() {
            $this->setSource("users");
        }

        public function login() {
            var_dump($this->toArray());
        }

    }

Trying to make this as simple as possible, but for some reason when I call User::findFirst, it's thinking I'm looking for a controller named User... Not the model.

  1. Start using IDE.
  2. Add use Zipline\Models\User; in controller after namespace


17.5k

Thanks, didn't realize you had to add the use statement in the controller.

Is it better to go this route or go back to not using namespaces? In my previous Phalcon apps I didn't have to specify the models in the use statements, but I also just loaded the directories instead of registering namespaces.

It's definitely better to seperate your application to namespaces. Just use some IDE :)



17.5k

Using PHP Storm, but it doesn't do anything like that for me.

edited Sep '16

What ? Imgur



17.5k

Yeah, nope... I don't get that for some reason. Not sure wny.

Maybe you have disabled inspection about it in options ?