views:

663

answers:

2

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.

+5  A: 

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);
    }  
  }
kuba
That seems to be what i was looking for... And welcome to Stack Overflow :-)
prodigitalson
The code you posted worked perfectly. Thanks!
prodigitalson
Nice, neat answer :-)
richsage
A: 

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.

Tom
Well you can customize the model name... And yes that will work out of the box to retrieve the profile however if not manually joined when you query it will issue a seperate query to get the profile object - im trying to make the join automatic (essentially turning off lazy load for the profile model and no others.
prodigitalson