我正在使用 Symfony 1.4 和 Doctrine 1.2 和 sfDoctrineGuardPlugin。我有一个文章创建页面。在我的文章表上,我有 profile_id 字段用于保存哪个用户(作者)写了它。
我的问题是,当用户写文章并点击保存时,即使我可以从用户那里获取 profile_id,我也无法自动设置 profile_id。
在我的 articleForm 类中,我可以为选择框设置默认值,但用户可以更改它。如果我尝试将其设为只读,则 profile_id 无法保存在数据库中。
这是我在 articleForm 类中的配置函数:
// 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']);
为了使其只读,我使用了:
$this->offsetUnset("profile_id");
但正如我之前所说,这样,profile_id 无法保存在数据库中。如果没有第二部分代码,当前 id 是默认值,但用户可以更改它。
怎么解决?任何想法?非常感谢...