symfony1symfony-1.4sfguardsfdoctrineguard

Get and list users based on idempresa of logged in user


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?


Solution

  • 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());
    
        //...
    }