开发者

Using sfDoctrineGuardPlugin. Want to set id field without user choice

I am using Symfony 1.4 and Doctrine 1.2 and sfDoctrineGuardPlugin. I have a article create pa开发者_开发百科ge. 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...


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜