Hi there,

When I try to declare a Model twice, the second declaration returns empty shared dependenicies. I'm probably missing something. Here what I have.

  • Library - LibraryBase.php
  • Library - User.php
  • Model - ModelBase.php
  • Model - UserGroupsModel.php
  • Controller - UsersController.php

ModelBase.php

namespace Models;

class ModelBase extends \Phalcon\Mvc\Model{

   public $db; // Setting up db connection

   // Initialize model
   function initialize()
   {
      $this->db=$this->getDI()->getShared("db"); // Setting up db connection from injector
   }

}

UserGroupsModel.php

namespace Models;

class UserGroupsModel extends ModelBase{

   function initialize()
   {
      parent::initialize();   
   }

   function test()
   {
      $this->db->query('SOME QUERY');
   }

}

LibraryBase.php

namespace Libraries;

class LibraryBase extends \Models\ModelBase {

    function initialize()
     {
        parent::initialize();
     }

}

User.php

namespace Libraries;

class User extends LibraryBase {
    function initialize()
     {
        parent::initialize();
     }

     function get_group()
     {
        $UserGroupsModel=new \Models\UserGroupsModel();
        $UserGroupsModel->test();
     }
}

UsersController.php

namespace Controllers;

class UsersController extends \Phalcon\Mvc\Controller{

    function userGoupsAction()
    {
        $UserGroupsModel=new \Models\UserGroupsModel();
        $UserGroupsModel->test();

        *********************************
        Error here. Says that query() is on non-object in the UserGroupsModel.
    }
}

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. First call for UserGroupsModel() is done from User.php library and it works. The second call for UserGroupsModel() is done from UserController.php and it returns empty $this->db in the model.

If not to extend ModelBase and manually assing objects using $this->db=$this->getDI()->getShared("db"); in the UserGroupsModel() then it works. But then you need to assing common objects in every Model. I'm probably missing something.

Any help, suggestions or solutions are highly appreciated.

Thank you in advance.

EDIT:

Here is the link to stackoverflow where I have a sample that triggers an error:http://stackoverflow.com/questions/23303450/phalcon-mvc-model-called-twice-returns-empty-shared-objects