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

How call method of Model from another Module / Как использовать модель другого модуля?

Hi, I have models/Users.php in Module - auth. I want to call method new Users -> getUser() in Module - profile

How can I do?

P.S.

/modules/auth/models/Users.php

public function getUser($username)
    {
        $user = Users::findFirst("id = '44'");
        return $user;
    }

/modules/profile/Module.php


$loader->registerNamespaces(array(
            'Auth\Models' => __DIR__ . '/../auth/models/'
        ));

/modules/profile/controller/indexController.php

$profile = new \Auth\Models\Users();
$profile->getUser($username);
//This return all fields, but it with value = null

But, if i write in modules/profile/indexController:

Users::findFirst("id = '44'"));
//Return all fields with correct data

Help me please :) I want to call model from another Module.

Sorry for my English :)

How can I use PHQL in Models?

What I do is very simple. Just register new namespace for models you need use inside your module:

$loader->registerNamespaces(array(
    __NAMESPACE__ . '\\Controllers' => __DIR__ . '/controllers/',
    __NAMESPACE__ . '\\Models' => __DIR__ . '/models/',
    //users
    'App\Users\Models' => dirname(__DIR__) . '/users/models/'
));

But do you have really strong reason do that? Cant you just use relation between models?

edited Sep '14

Thanks, it is work, use

Users::getUser($username);

No, i am not use relation. I have two modules working with one a database table.

$profile = new \Auth\Models\Users();
$profile->getUser($username);
**//This return all fields, but it with value = null**

I think that this is bug, because:

Users::getUser($username)

working! But maybe I'm wrong.

Well tell you truth Im not sure what are you doing or whats inside of your models. Especially you didnt describe your function getUser (because you said you dont use relation between models) so perhaps problem is inside your function. How do you get user by username? Something like:

Users::findFirst(array(
    'conditions' => 'username = ?1',
    'bind' => array(1 => $username)
));