title: Trying to connect to database
body: <p>I'm working on a rewrite of a project from the ground up and figured I would try to learn MVC along the way. In this case, I've chosen Phalcon and am still working through the fundamentals of converting the tutorials to my own project. </p>
  
  <p>I have two "configuration" settings that I need to account for.  First, I need to read a configuration file that has the database credentials (this works properly).  </p>
  
  <pre><code>require_once('../fileconfig.php'); // Read config file
  $init = new Phalcon\Config\Adapter\Php("../fileconfig.php"); //Convert it to array
  </code></pre>
  
  <p>But once I have that, how do I actually connect to the database and add it to $di-> (which, if I understand correctly, is effectively the global class?  Ultimately, I want to pull the contents of "select * from config" into an array and use that for the application configuration.  In this case, var_dump($dbh) returns "null"</p>
  
  <pre><code>//Connect to database
  $di-&gt;set('db', function() use ($init) {
      $dbh = new \Phalcon\Db\Adapter\Pdo\Mysql([
          "host" =&gt; $init-&gt;database-&gt;host,
          "username" =&gt; $init-&gt;database-&gt;username,
          "password" =&gt; $init-&gt;database-&gt;password,
          "dbname" =&gt; $init-&gt;database-&gt;dbname
      ]);
      return $dbh;
  });
  var_dump($dbh);  //returns null
  </code></pre>
  
  <p>If I remove the $di-> section, the array returns the data that I need, but it still doesn't help me figure out how to connect to the database and have it available globally for other functions in the models:</p>
  
  <pre><code>    $dbh = new \Phalcon\Db\Adapter\Pdo\Mysql([
          "host" =&gt; $init-&gt;database-&gt;host,
          "username" =&gt; $init-&gt;database-&gt;username,
          "password" =&gt; $init-&gt;database-&gt;password,
          "dbname" =&gt; $init-&gt;database-&gt;dbname
      ]);
  </code></pre>
  
  <p>Returns:</p>
  
  <pre><code>object(Phalcon\Db\Adapter\Pdo\Mysql)[28]
    protected '_descriptor' =&gt; 
      array (size=4)
        'host' =&gt; string 'localhost' (length=9)
        'username' =&gt; string 'testuser' (length=8)
        'password' =&gt; string 'testpass' (length=8)
        'dbname' =&gt; string 'testdb' (length=6)
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/25285851/how-to-properly-initialize-database-connection-in-phalcon">This question</a> seems to be close to what I'm asking, but was more about error handling than the actual connection and I didn't see an answer to my question there.</p>