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.

Does not load related model, i am using many to many relationship.

I am getting an error which says Phalcon\Mvc\Model\Manager can not load the model. I used multi module setup.

Having multi module setup.

My Models:

  1. Users
  2. Groups
  3. GroupsMembers

Relationship in Users model:

public function initialize(){

$this->hasManyToMany("id", 
               "GroupsMembers",
               "user_id","group_id",
               "Groups",
               "id",
                array('alias' => 'GroupsMembers')
                );

}

Relationship in Groups model:

public function initialize(){

    $this->hasManyToMany("id", 
                         "GroupsMembers",
                         "group_id","user_id",
                         "Users",
                         "id",
                          array('alias' => 'Users')
                        );

}

Relationship in GroupsMembers model:

public function initialize(){

    $this->belongsTo("group_id", "Groups", "id",  array('alias' => 'Group'));
    $this->belongsTo("user_id", "Users", "id",  array('alias' => 'User'));

}

Controller where I am fetching list of groups which is subscribed by a user.

The top part of my controller where I have included all these 3 models and expected that these would be avilable to use.

namespace Uno\Webservice\Controllers; use Uno\Webservice\Models\Users; use Uno\Webservice\Models\Groups; use Uno\Webservice\Models\GroupsMembers;

class UserController extends ControllerBase {

public function indexAction()
{
    $this->view->disable();
    $user_id = 3;
    $user = Users::findFirstById($user_id);

    $subscribed_groups = $user->groupsMembers;

    foreach($subscribed_groups as $groupMember){
        echo $groupMember->groups->title;
    }
    die;
}

The error I am getting is:

Fatal error: Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'Model 'Groups' could not be loaded' in /var/www/html/uno/apps/webservice/controllers/UserController.php:245 Stack trace: #0 [internal function]: Phalcon\Mvc\Model\Manager->load('Groups') #1 [internal function]: Phalcon\Mvc\Model\Query->prepareSelect() #2 [internal function]: Phalcon\Mvc\Model\Query->parse() #3 [internal function]: Phalcon\Mvc\Model\Query->execute() #4 [internal function]: Phalcon\Mvc\Model\Manager->getRelationRecords(Object(Phalcon\Mvc\Model\Relation), NULL, Object(Uno\Webservice\Models\Users), NULL) #5 /var/www/html/uno/apps/webservice/controllers/UserController.php(245): Phalcon\Mvc\Model->_get('groupsMembers') #6 [internal function]: Uno\Webservice\Controllers\UserController->followedGroupsAction() #7 [internal function]: Phalcon\Dispatcher->dispatch() #8 /var/www/html/uno/public/index.php(196): Phalcon\Mvc\Application->handle() #9 /var/www/html/uno/public/index.php(202): Application->main() #10 {main} thrown in /var/www/html/uno/apps/webservice/controllers/UserController.php on line 245

Please let me know if any more information needed to answer that. Any help on that would be apperciable.

Best, Rakesh



1.4k

shouldn't it be group? since its alias group?

I changed the alias to Groups, it did not work :(



27.4k
Accepted
answer

Hi,

I think that you have to declare full namespaced Models in your relations:

$this->belongsTo("user_id", "Uno\Webservice\Models\Users", "id", array('alias' => 'User'));

Thank you, it worked with full namespace model name.