Using sfDoctrineGuardPlugin. Want to set id field without user choice

478 Views Asked by At

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:

// 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:

$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...

1

There are 1 best solutions below

1
On

you are on the wrong track pal, if you want to save default value, no need to expose it in the form to be able to be posted so you can save. Instead, you should have the logic at your application. Just set whatever value you would like to be default before you save the object. You can make use of Peer class as well peacefully by using preInsert method.