Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

model manager stops the website

Hello frieds :)

when I call this code, the website does not work and blank page appear:

$settings = array();

    $manager = $this->modelsManager;
  $globalSettings = $manager->executeQuery('SELECT * FROM settings');
  foreach ($globalSettings as $settingsData) {
      $settings[] = array(
          'initial_balance' => $settingsData->initial_balance,
          'site_mail' => $settingsData->site_mail
      );
  }

what could be a problem?

thank you :)



5.7k
Accepted
answer
edited Nov '14

Do you have error 404 page set up in your web app? I believe that it is showing you that error page. Because I had same problem with my app. You are trying to use this query for PDO option and other stuff. As far I know, this is not PHQL. And to access this modelManager, you will need few more lines of codes in DI services. I couldn't figure out this problem and I had same question that I asked here. So, try to read everything there and solution that Phalcon user, (one of the developers) gave me. But still, I suggest you to use your code like below if your these codes are in controller. (and you will need something to select row from table)

// Get and print global setttings to print details
$globalSettings  = Settings::find(array(
    "initial_balance  < '300'"
));
foreach ($globalSettings  as $settingsData) {
    $settings[] = array(
      'initial_balance' => $settingsData->initial_balance,
      'site_mail' => $settingsData->site_mail
  );
}

So it means, you are trying to find a row where initial balance is lower then 300 and it will fetch all data from that row. Its not perfect way because there might be more then one row in your database so you can do like

  "initial_balance  < '300'",
  "username = 'myName'"

in your PHQL so you will get accutrate result. Try this. If it doesn't work, let me know. ;) Enjoy your codes.

And also add this codes in your services file at located config/services.php.

//trying to display error pages
$di->set('dispatcher', function() {

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

$eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {

        //Handle 404 exceptions
        if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
            $dispatcher->forward(array(
                'controller' => 'error',
                'action' => 'show404'
            ));
            return false;
        }

        //Handle other exceptions
        $dispatcher->forward(array(
            'controller' => 'error',
            'action' => 'show503'
        ));

        return false;
    });

    $dispatcher = new \Phalcon\Mvc\Dispatcher();

    //Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;

}, true);

It will help you to show error pages like 404 and 503 etc. Create error class and a method to show 404 error page. ;)

Just like a magic (y) Thank you very much Viral Joshi, by the way, my default code works on Windows, but I had to edit it according to your replay in order to work also on Linux

Thank you again :)

Your welcome Feras Jobeir.... ;) As I said, enjoy your codes... :D :D I do enjoy mine. ( Somehow, they are working. :P )

Your welcome Feras Jobeir.... ;) As I said, enjoy your codes... :D :D I do enjoy mine. ( Somehow, they are working. :P )