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

Error selecting a db

Hi guys,

I have a generic class from which I'm planning to connect to my db. The code is the following:

<?php

namespace CMS\app\dal;

class MongoDbDal{ private $db = "";

function __construct(){
    $config = include __DIR__ . "/../config/config".( getenv('CMS_ENV')? "_".getenv('CMS_ENV'): '' ).".php";

    if (!$config->mongo->username OR !$config->mongo->password) {
        $mongo = new \MongoClient('mongodb://' . $config->mongo->host);
    } else {

        if ( isset($config->mongo->rplSet) ) {
            $mongo = new \MongoClient("mongodb://" . $config->mongo->connection_string .'/'. $config->mongo->dbname.'?replicaSet='.$config->mongo->rplSet);

        } else {
            $mongo = new \MongoClient("mongodb://" . $config->mongo->username . ":" . $config->mongo->password . "@" . $config->mongo->host.'/'. $config->mongo->dbname);
        }

    }
    $this->$db = $mongo->selectDb($config->mongo->dbname);
}

public function listarNotas(){
    return $this->db;
}

}

The programs fails in this line:

$this->$db = $mongo->selectDb($config->mongo->dbname);

but I can't capture de error with try catch. I don't know why. What could be the possible error? Thanks for your help!



43.9k

Hi,

what's the error you get (see webserver log) ?