prodigitalson
prodigitalson

Reputation: 60413

sfDoctrineGuard - how to ALWAYS join sfGuardProfile to sfGuardUser

I want to make it so that anytime the db is queried for an sfGuardUserProfile it is autmoatically joined and hydrated with its related sfGuardUser.

If i was using Propel 1.2 i would normally override the doSelectStmt method of the sfGuardUserProfilePeer class to inspect the Criteria and modify it as necessary as well as modifying the hydrate method of the sfGuardUserProfile class. Im not sure how to go about doing this in Doctrine though.

Upvotes: 1

Views: 2259

Answers (2)

Jakub Zalas
Jakub Zalas

Reputation: 36191

You could use Event Listeners. Read more about them in the doctrine documentation: Event Listeners

In symfony 1.4 sfGuardUser can be modified. It's by default in lib/model/doctrine/sfDoctrineGuardPLugin/sfGuardUser.class.php. You can add following preDqlSelect() method to modify the query. Note that it's not tested.

class sfGuardUser extends PluginsfGuardUser
{
  public function preDqlSelect($event)
  {
    $params = $event->getParams();
    $query  = $event->getQuery();
    $alias  = $params['alias'] . '.Profile';
    if ((!$query->isSubquery() || ($query->isSubquery() && $query->contains(' ' .     $params['alias'] . ' '))) && !$query->contains($alias)) 
    {   
      $query->innerJoin($alias);
    }   
  }
}

To make it working you need to have DQL callbacks turned on. You can do it in your ProjectConfiguration class:

  class ProjectConfiguration extends sfProjectConfiguration
  {
    public function configureDoctrine(Doctrine_Manager $manager)
    {  
      $manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
    }  
  }

Upvotes: 8

Tom
Tom

Reputation: 30698

Although I agree with Coronatus, I think what you're looking to do can be achieved with:

http://www.symfony-project.org/plugins/sfGuardPlugin

See "Customize the sfGuardUser model".

Basically, the profile needs to be called "sf_guard_user_profile" and the relation set up, and then you should be able to use:

$this->getUser()->getGuardUser()->getProfile();

I think the right profile model name is needed for some config file purposes but I may be wrong.

Upvotes: 1

Related Questions