开发者

sfDoctrineGuard - how to ALWAYS join sfGuardProfile to sfGuardUser

开发者 https://www.devze.com 2022-12-29 00:23 出处:网络
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.

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 a开发者_如何转开发nd 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.


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);
    }  
  }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消