0

I want to get & list all the users that belongs to the same idempresa that the user logged in has. idempresa is related to users in a sf_guard_profile table and yes I have the relation declared on schema.yml file. I think in write a method in sfGuardUserTable.class.php but I'm not so sure it will works in others words I don't know where to touch or what to handle this. Can any point me in the right direction on this? This is the method (unfinished):

public function getUsersByCompany() {
        $user = sfContext::getInstance()->getUser()->getProfile()->getIdEmpresa();
        $q = $this->createQuery('g');

        return $q;
}

Any advice?

Reynier
  • 2,420
  • 11
  • 51
  • 91

1 Answers1

2

It's generally not good practice to use sfContext::getInstance(). Read this post http://webmozarts.com/2009/07/01/why-sfcontextgetinstance-is-bad/.

How about adding a static method to your sfGuardUserTable.class.php, e.g

// \lib\form\sfGuardUserTable.class.php
static public function getUsersByIdEmpresa($idempresa)
{
    //...
    $q->andWhere('idempressa = ?', $idempresa);

    return $q->execute(); 
}

Then calling it in your controller or from where you need, e.g

// \app\myApp\modules\myModule\actions\action.class.php
public function executeMyAction() {
    $profile = $this->getUser()->getProfile();
    $usersRelatedByIdempresa = sfGuardUserTable::getUsersByIdempresa($profile->getIdempresa());

    //...
}
antony
  • 2,763
  • 19
  • 23
  • get your point but I get lost, if I'm talking about `sfDoctrineGuard` plugin then you say that I must overwrite the method `executeIndex()` under `admin/modules/sfGuardUser/actions`? – Reynier Jun 09 '13 at 13:32
  • That was just an example of how you can call it in a controller. You don't have to overwrite the sfGuardUser actions. – antony Jun 10 '13 at 03:30