This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Using sfDoctrineGuardPlugin. Want to set id field without us

Ask help about existing plugins, or find advice about the way to build up your own.

Using sfDoctrineGuardPlugin. Want to set id field without us

by erman » Mon Jan 24, 2011 5:30 am

Hi all.

I am using Symfony 1.4 and Doctrine 1.2 and sfDoctrineGuardPlugin. I have a article create page. On my article table, i have profile_id field for saving which user (author) wrote it.

My problem is, when a user write an article and click to save, even if i can get profile_id from user, i cant set profile_id automatically.

In my articleForm class, i can set default value for select box, but user can change it. If i try to make it read only, profile_id cannot be saved on db.

Here is my configure function from articleForm class:

Code: Select all
// getting user_id $user = sfContext::getInstance()->getUser(); $user_id = $user->getGuardUser()->getId();

// converting for profile table
$converter = Doctrine_Query::create()
            ->select('*')
            ->from('profile')
            ->where('sf_guard_user_id = ?', $user_id);

// fetching
$result = $converter->fetchArray();

$this->widgetSchema->setDefault('profile_id', $result[0]['id']);

To make it read only, i used:

Code: Select all
$this->offsetUnset("profile_id");


But as i said before, this way, profile_id cannot saved on db. Without second part of code, current id came as default but users can change it.

How can solve it? Any idea? Thanks a lot...
erman
Junior Member
 
Posts: 6
Joined: Fri Dec 31, 2010 3:27 pm

Re: Using sfDoctrineGuardPlugin. Want to set id field withou

by JPG » Fri Apr 20, 2012 12:33 pm

Old post, I know, but I found it yesterday when I was having a similar problem. What you need to do is make the widget hidden so the user can't change it e.g. in the form class configure() method:
Code: Select all
$this->setWidget('profile_id', new sfWidgetFormInputHidden());


If you set the default for the widget, this value should be loaded into the db when you save the form. (Make sure you use $form -> renderHiddenFields() in your template).

I also had the additional requirement that the field should indicate the user updating a record in the case of an edit. The problem here is that the default is only used if there isn't already a value, so for an edit, the widget would contain the existing value in the db (i.e. the user who created or last updated the record). To change this value automatically, you need to update the object in the action class before saving the form as below...

Code: Select all
public function executeEdit(sfWebRequest $request)  {
      ...
      $this->forward404Unless($article = Doctrine_Core::getTable('Articles')->find(array($request->getParameter('article_id'))), sprintf('Object article does not exist (%s).', $request->getParameter('article_id')));
   
      // do what you have to to get $id e.g.
      $converter = Doctrine_Query::create()
            ->select('*')
            ->from('profile')
            ->where('sf_guard_user_id = ?', $this->getUser()->getUserID());

       // fetching
       $result = $converter->fetchArray();
       $id = $result[0]['id'];

      $article->setProfileId($id)
      $this->form = new ArticleForm($article);
}


In my hands, it didn't work unless I did the same thing in the executeUpdate() method.
JPG
Junior Member
 
Posts: 3
Joined: Wed Nov 23, 2011 1:35 pm