我使用的Symfony 1.4和條令1.2和sfDoctrineGuardPlugin。我有一篇文章創建頁面。在我的文章表上,我有profile_id字段用於保存哪個用戶(作者)寫了它。使用sfDoctrineGuardPlugin。要設置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作爲默認值,但用戶可以更改它。
該如何解決呢?任何想法? 非常感謝...
您應該在表單中使用`sfContext :: getInstance()`,而不是在構造函數中注入用戶對象或user_id值。此外,這應該有所幫助:http://stackoverflow.com/questions/4742099/symfony-admin-generator-add-user-id-before-saving/4744077#4744077 – Maerlyn 2011-01-24 10:16:36
是的,這就是我所期待的。非常感謝Maerlyn – 2011-01-26 03:38:28