So using the context object is the easiest way, but you could
definitely add dependency injection, it just take a little more code.
However, and I'm not certain on this, but you should be able to unit
test this as the sfContext object is available during testing.  I
think the idea is that if you had different objects that represented
users, you couldn't test each one.  Like the example from your link,
it is talking about using different storage methods, for each of which
there is a specific class.  If that data was pulled from the context
it would be hard to test each individual type of storage without
writing a separate test for each.

Since I normally find myself only using the integrated user with
symfony or an object from the DB, I don't really bother with DI,
unless I think I will be acting on a user other than the one who
requested the page.  In that case I would add a user member to the
form class and pass the user to the constructor.

This is what I would do:

private $currentUser;

public function __construct($user,$defaults = array(), $CSRFSecret =
null){
  $this->currentUser = $user;
  parent::__construct($defaults,$CSRFSecret);
}

public function configure(){
   parent::configure();
   if(!is_null($this->currentUser)){
        $this->getObject()->user_id = $this->currentUser->id;
   }else throw new Exception('You must supply a valid user object);
}

HTH,
Casey


On May 5, 12:54 am, Sela Yair <tzi...@gmail.com> wrote:
> one concern related to your solution is something i read about Dependency
> injectionhttp://fabien.potencier.org/article/11/what-is-dependency-injectionby
>  using
> the sfContext i think i would eliminate using unit testing as the configure
> method isn't independent any longer.
> am I right?
>
> On 4 May 2010 23:58, Sela Yair <tzi...@gmail.com> wrote:
>
>
>
>
>
> > thanks, that what i was after.
>
> > On 4 May 2010 18:27, Casey <casey.cam...@gmail.com> wrote:
>
> >> Doctrine make this easier.  The way you are doing it works, but it may
> >> be harder to maintain in the future.  I would reccommend something
> >> like this:
>
> >> MemberForm.class.php::
> >> function configure(){
> >>   parent::configure();
> >>   $this->getObject()->user_id = sfContext::getInstance()->getUser()-
> >> >getGuardUser()->id;
> >> }
>
> >> When this form saves it will save the Member object with the current
> >> users user_id.  It is best to keep this kind of logic in the form,
> >> specifically the configure() method, it will be much easier in the
> >> future if you perform as little logic as possible in the action or the
> >> view.
>
> >> HTH,
> >> Casey
>
> >> On May 4, 4:27 am, Sela Yair <tzi...@gmail.com> wrote:
> >> > I think I found slick solution, would like to get some feedback if
> >> that's
> >> > the ideal solution.
>
> >> > in action.class.php
>
> >> > i modified the $form->bind
>
> >> >  *protected function processForm(sfWebRequest $request, sfForm $form)*
> >> > *  {*
> >> > *    $formData = $request->getParameter($form->getName());*
> >> > *    $formData['user_id'] = $this->getUser()->getGuardUser()->getId();*
> >> > *    $form->bind($formData, $request->getFiles($form->getName()));*
> >> > *    if ($form->isValid())*
> >> > *    {*
>
> >> > On 4 May 2010 10:50, Sela Yair <tzi...@gmail.com> wrote:
>
> >> > > Thanks.
> >> > > Yes that's what I'm after.
>
> >> > > the following is the table in schema.yml
> >> > > Member:
> >> > >   actAs:
> >> > >     Timestampable: ~
> >> > >   columns:
> >> > >     date_birth:
> >> > >       type: date
> >> > >       notnull: true
> >> > >     logo:
> >> > >       type: string(255)
> >> > >     is_public:
> >> > >       type: boolean
> >> > >       notnull:  true
> >> > >       default:  1
> >> > >     is_activated:
> >> > >       type: boolean
> >> > >       notnull:  true
> >> > >       default:  0
> >> > >     country_id:
> >> > >       type: string(2)
> >> > >     user_id:
> >> > >       type: integer(4)
> >> > >   relations:
> >> > >     sfGuardUser: { foreign: id, local: user_id, onDelete: cascade }
>
> >> > > Someone can insert its details only if s/he's registered, and i want
> >> the
> >> > > user_id to be populated with the current user id. i managed to do it
> >> by
> >> > > updating a property in the lib/model and when using Save method
> >> changing
> >> > > this column, but i believe there's more elegant way to do it.
>
> >> > > On 4 May 2010 10:09, Tom Ptacnik <to...@tomor.cz> wrote:
>
> >> > >> Yes you can do a relation between a user and another table object. Is
> >> > >> that what you want?
> >> > >> Please describe you question again..
>
> >> > >> On 3 kvě, 14:37, Sela <tzi...@gmail.com> wrote:
> >> > >> > I want to associate a table with the user ID generated from
> >> > >> > sfDoctrineGuardPlugin when the user is logged in. I thought to do
> >> it
> >> > >> > in save() but not sure how to get the userID at that point as it's
> >> in
> >> > >> > lib/model/doctrine and in the plugin readme they explain how to get
> >> > >> > the value only from the template or the action.
>
> >> > >> > --
> >> > >> > If you want to report a vulnerability issue on symfony, please send
> >> it
> >> > >> to security at symfony-project.com
>
> >> > >> > You received this message because you are subscribed to the Google
> >> > >> > Groups "symfony users" group.
> >> > >> > To post to this group, send email to
> >> symfony-users@googlegroups.com
> >> > >> > To unsubscribe from this group, send email to
> >> > >> > symfony-users+unsubscr...@googlegroups.com<symfony-users%2bunsubscr...@goog
> >> > >> >  legroups.com>
> >> <symfony-users%2bunsubscr...@goog legroups.com>
> >> > >> > For more options, visit this group athttp://
> >> > >> groups.google.com/group/symfony-users?hl=en
>
> >> > >> --
> >> > >> If you want to report a vulnerability issue on symfony, please send
> >> it to
> >> > >> security at symfony-project.com
>
> >> > >> You received this message because you are subscribed to the Google
> >> > >> Groups "symfony users" group.
> >> > >> To post to this group, send email to symfony-users@googlegroups.com
> >> > >> To unsubscribe from this group, send email to
> >> > >> symfony-users+unsubscr...@googlegroups.com<symfony-users%2bunsubscr...@goog
> >> > >>  legroups.com>
> >> <symfony-users%2bunsubscr...@goog legroups.com>
> >> > >> For more options, visit this group at
> >> > >>http://groups.google.com/group/symfony-users?hl=en
>
> >> > --
> >> > If you want to report a vulnerability issue on symfony, please send it
> >> to security at symfony-project.com
>
> >> > You received this message because you are subscribed to the Google
> >> > Groups "symfony users" group.
> >> > To post to this group, send email to symfony-users@googlegroups.com
> >> > To unsubscribe from this group, send email to
> >> > symfony-users+unsubscr...@googlegroups.com<symfony-users%2bunsubscr...@goog
> >> >  legroups.com>
> >> > For more options, visit this group athttp://
> >> groups.google.com/group/symfony-users?hl=en
>
> >> --
> >> If you want to report a vulnerability issue on symfony, please send it to
> >> security at symfony-project.com
>
> >> You received this message because you are subscribed to the Google
> >> Groups "symfony users" group.
> >> To post to this group, send email to symfony-users@googlegroups.com
> >> To unsubscribe from this group, send email to
> >> symfony-users+unsubscr...@googlegroups.com<symfony-users%2bunsubscr...@goog
> >>  legroups.com>
> >> For more options, visit this group at
> >>http://groups.google.com/group/symfony-users?hl=en
>
> --
> If you want to report a vulnerability issue on symfony, please send it to 
> security at symfony-project.com
>
> You received this message because you are subscribed to the Google
> Groups "symfony users" group.
> To post to this group, send email to symfony-users@googlegroups.com
> To unsubscribe from this group, send email to
> symfony-users+unsubscr...@googlegroups.com
> For more options, visit this group 
> athttp://groups.google.com/group/symfony-users?hl=en

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to