title: Phalcon MVC Model called twice returns empty shared objects
body: <p>When I try to declare a Model twice, the second declaration returns empty shared dependencies. I'm probably missing something. Here what I have.</p>
  
  <ul>
  <li>Library - LibraryBase.php</li>
  <li>Library - User.php</li>
  <li>Model - ModelBase.php</li>
  <li>Model - UserGroupsModel.php</li>
  <li>Controller - UsersController.php</li>
  </ul>
  
  <p>ModelBase.php</p>
  
  <pre><code>namespace Models;
  
  class ModelBase extends \Phalcon\Mvc\Model{
  
     public $db; // Setting up db connection
  
     // Initialize model
     function initialize()
     {
        $this-&gt;db=$this-&gt;getDI()-&gt;getShared("db"); // Setting up db connection from injector
     }
  
  }
  </code></pre>
  
  <p>UserGroupsModel.php</p>
  
  <pre><code>namespace Models;
  
  class UserGroupsModel extends ModelBase{
  
     function initialize()
     {
        parent::initialize();   
     }
  
     function test()
     {
        $result=$this-&gt;db-&gt;query('SELECT * FROM user_groups');
        if($result-&gt;numRows()&gt;0)
        {
           $data=$result-&gt;fetchAll();
           var_dump($data);
        }
     }
  
  }
  </code></pre>
  
  <p>LibraryBase.php</p>
  
  <pre><code>namespace Libraries;
  
  class LibraryBase extends \Models\ModelBase {
  
     function initialize()
     {
        parent::initialize();
     }
  
  }
  </code></pre>
  
  <p>User.php</p>
  
  <pre><code>namespace Libraries;
  
  class User extends LibraryBase {
      function initialize()
      {
         parent::initialize();
      }
  
      function get_group()
      {
        $UserGroupsModel=new \Models\UserGroupsModel();
        $UserGroupsModel-&gt;test();
      }
  
      function access()
      {
         $this-&gt;get_group();
      }
  }
  </code></pre>
  
  <p>UsersController.php</p>
  
  <pre><code>namespace Controllers;
  
  class UsersController extends \Phalcon\Mvc\Controller{
  
     function initialize()
     {
        $this-&gt;user-&gt;access(); // Checking user access
     }
  
     function UserGroupsAction() // User group action
     {
         $UserGroupsModel=new \Models\UserGroupsModel();
         $UserGroupsModel-&gt;test();
     }
  }
  </code></pre>
  
  <p>index.php</p>
  
  <pre><code>// Bootstrap 
  try {
  
      //Register an autoloader
      $loader = new \Phalcon\Loader();
  
      $loader-&gt;registerNamespaces([
           'Controllers' =&gt;'app/controllers/',
           'Models' =&gt;'app/models/',
           'Libraries'=&gt;'app/libraries/'
      ])-&gt;register();
  
      //Create a DI
      $di = new Phalcon\DI\FactoryDefault();
  
      // Setting User Class
      $di-&gt;set("user", function (){       
         $user=new \Libraries\User(); // Initiates class
         return $user;       
      });
  
      //Setup the database service
      $di-&gt;set('db', function() use ($config){
          return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
              "host" =&gt; "localhost",
              "username" =&gt; "root",
              "password" =&gt; "123456",
              "dbname" =&gt; "database",
              'options' =&gt; [PDO::ATTR_PERSISTENT =&gt; FALSE,PDO::ATTR_DEFAULT_FETCH_MODE=&gt;PDO::FETCH_ASSOC],
           ));
      });
  
      //Setup the view component
      $di-&gt;set('view', function(){
          $view = new \Phalcon\Mvc\View();
          // PHP files for as view files
          $view-&gt;registerEngines(array(
              ".php" =&gt; "Phalcon\Mvc\View\Engine\Php"
           ));
          $view-&gt;setViewsDir('app/views/'); // Setting a desktop version path first. If mobile device is used, redirect to mobile directory
          return $view;
      });
  
      // Setup Router
      $di-&gt;set('router', function(){
  
           $router = new \Phalcon\Mvc\Router(false);
  
           $router-&gt;removeExtraSlashes(true);
           $router-&gt;setDefaultNamespace("Controllers");
           $router-&gt;setDefaultAction("index");
           $router-&gt;notFound(array("controller"=&gt;"errors","action"=&gt;"e404","namespace"=&gt;"Controllers"));
  
           // Add root controller
           $router-&gt;add('/:controller/:action', array(
              'namespace' =&gt; 'Controllers',
              'controller' =&gt; 1,
              'action'=&gt;2 
           ));         
  
           return $router;
      });
  
      //Handle the request
      $application = new \Phalcon\Mvc\Application($di);
  
      echo $application-&gt;handle()-&gt;getContent();
  
  } catch(\Phalcon\Exception $e) {
  
        echo "PhalconException: ", $e-&gt;getMessage();
        echo "&lt;br&gt;";
        var_dump($e);
  }
  </code></pre>
  
  <p>The idea is to extend ModelBase and to have access to common dependencies. Users.php (library) is processing user authentication etc. UsersController.php dealing with users. LibraryBase extends ModelBase to access common objects. </p>
  
  <p>First call for <code>UserGroupsModel()</code> is done from User.php library and it works. The second call for <code>UserGroupsModel()</code> is done from UserController.php and it returns empty <code>$this-&gt;db</code> object in the model.</p>
  
  <p>If not extending <code>ModelBase</code> and manually assign objects using <code>$this-&gt;db=$this-&gt;getDI()-&gt;getShared("db");</code> in the <code>UserGroupsModel()</code> then it works. But then you need to assign common objects in every Model. I'm probably missing something.</p>
  
  <p>EDIT:<br>
  I have updated the codes, now it shows error if you are running this example.</p>
  
  <p>Thank you in advance.</p>