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

count bug

class Users extends \Phalcon\Mvc\Model { public function getSource() { return 'mk_user'; }

}

echo Test\Models\Users::count();

Undefined property: Phalcon\Config::$cache



3.9k
edited Oct '14

error_reporting(E_ERROR);

;)

Maybe u forgot some variable $cache in Phalcon initialization?



98.9k

The ORM doesn't use the Config component so I'm pretty sure that notification is not related with Mvc\Model. Could you please post more code to take a look at the error?



3.9k
edited Oct '14
<?php

error_reporting(E_ERROR);
//error_reporting(0);
if (!defined('ROOT_PATH')) {
    define('ROOT_PATH', dirname(dirname(__FILE__)));
}

try {

    //var_dump($_GET);
    //exit;
    /**
     * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
     */
    $di = new \Phalcon\DI\FactoryDefault();
    //
    //$config = new Phalcon\Config\Adapter\Ini(__DIR__.'/../common/config/config.ini');
    $config=require __DIR__.'/../common/config/config.php';
    $di->setShared('config', $config);
    /**
     * Registering a router
     */
    $di->set('router', function(){
        $host=explode('.',strtolower($_SERVER["HTTP_HOST"]));//SERVER_NAME
        if (count($host)<=1 || $host[0]=='www') $host[0]='home';
        $_GET['_url']=isset($_GET['_url'])?'/'.$host[0].$_GET['_url']:'/'.$host[0].'/';
        return require __DIR__.'/../common/config/routes.php';
    });
    /**
     * The URL component is used to generate all kind of urls in the application
     */
    $di->set('url', function(){
        $url = new \Phalcon\Mvc\Url();
        $url->setBaseUri('/');
        return $url;
    });

    /**
     * Start the session the first time some component request the session service
     */
    $di->set('session', function(){
        $session = new \Phalcon\Session\Adapter\Files();
        $session->start();
        return $session;
    });
    /**
     * If the configuration specify the use of metadata adapter use it or use memory otherwise
     */
    $di->set('modelsMetadata', function() use ($config) {
        if(isset($config['metadata'])){
            $metadataAdapter = 'Phalcon\Mvc\Model\Metadata\\'.$config['metadata']['adapter'];
            return new $metadataAdapter();
        } else {
            return new Phalcon\Mvc\Model\Metadata\Memory();
        }
    });

    $di->set('profiler', function(){
        return new Phalcon\Db\Profiler();
    });

    $di->setShared('modelsManager', function() {

        $eventsManager = new \Phalcon\Events\Manager();

        //Attach an anonymous function as a listener for "model" events
        $eventsManager->attach('model', function($event, $model){

            //Catch events produced by the Robots model
            if (get_class($model) == 'Robots') {

                if ($event->getType() == 'beforeSave') {
                    if ($modle->name == 'Scooby Doo') {
                        echo "Scooby Doo isn't a robot!";
                        return false;
                    }
                }

            }
            return true;
        });

        //Setting a default EventsManager
        $modelsManager = new \Phalcon\Mvc\Model\Manager();
        $modelsManager->setEventsManager($eventsManager);
        return $modelsManager;
    });

    //Set the views cache service
    $di->set('viewCache', function(){

        //Cache data for one day by default
        $frontCache = new Phalcon\Cache\Frontend\Output(array(
            "lifetime" => 0//86400
        ));

        //File backend settings
        $cache = new Phalcon\Cache\Backend\File($frontCache, array(
            "cacheDir" => __DIR__."/../runtime/cache/",
        ));

        return $cache;
    });
    $di->set('dispatcher', function() use ($di) {

            $evManager = $di->getShared('eventsManager');

            $evManager->attach(
                "dispatch:beforeException", function($event, $dispatcher, $exception){
                    switch ($exception->getCode()) {
                        case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                        case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                            $dispatcher->forward(array(
                                'controller' => 'index',
                                'action' => 'show404'
                            ));
                            return false;
                    }
            });

            //$security = new MallKeeper\Plugins\Security($di);

            /**
             * We listen for events in the dispatcher using the Security plugin
                 */
            //$evManager->attach('dispatch', $security);            
            $dispatcher = new \Phalcon\Mvc\Dispatcher();
            $dispatcher->setEventsManager($evManager);

            return $dispatcher;
        });
    /**
     * Main logger file
     */
    $di->set('logger', function() {
        return new \Phalcon\Logger\Adapter\File(__DIR__.'/../runtime/logs/'.date('Y-m-d').'.log');
    }, true);

    /**
     * Register the flash service with custom CSS classes
     */
    $di->set('flash', function(){
        return new Phalcon\Flash\Direct(array(
            'error' => 'alert alert-error',
            'success' => 'alert alert-success',
            'notice' => 'alert alert-info',
        ));
    });
    /**
     * Error handler
     */
    set_error_handler(function($errno, $errstr, $errfile, $errline) use ($di)
    {
        if (!(error_reporting() & $errno)) {
            return;
        }

        $di->getFlash()->error($errstr);
        $di->getLogger()->log($errstr.' '.$errfile.':'.$errline, Phalcon\Logger::ERROR);
        return true;
    });

    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application();
    $application->setDI($di);
    /**
     * Register application modules
     */
    $application->registerModules(require __DIR__.'/../common/config/modules.php');
    echo $application->handle()->getContent();

} catch (Phalcon\Exception $e) {
    echo $e->getMessage();
} catch (PDOException $e){
    echo $e->getMessage();
}


3.9k

Is this it? $config=require DIR.'/../common/config/config.php'; $di->setShared('config', $config);

Maybe in '/../common/config/config.php' you lost '$' symbol?