2011-08-06 61 views
0

我用Jobeet學習了Symfony 1.4。我爲用戶創建了Jobeet和系統登錄。現在我想添加可能性編輯自己的會員。當前用戶的action.class

<?php 

class jobActions extends sfActions 
{ 
    public function executeIndex(sfWebRequest $request) 
    { 
    $this->jobeet_job_list = Doctrine::getTable('JobeetJob') 
     ->createQuery('a') 
     ->execute(); 
    } 

    public function executeShow(sfWebRequest $request) 
    { 
    $this->jobeet_job = Doctrine::getTable('JobeetJob')->find($request->getParameter('id')); 
    $this->forward404Unless($this->jobeet_job); 
    } 

    public function executeNew(sfWebRequest $request) 
    { 
    $this->form = new JobeetJobForm(); 
    } 

    public function executeCreate(sfWebRequest $request) 
    { 
    $this->forward404Unless($request->isMethod('post')); 

    $this->form = new JobeetJobForm(); 

    $this->processForm($request, $this->form); 

    $this->setTemplate('new'); 
    } 

    public function executeEdit(sfWebRequest $request) 
    { 
    $this->forward404Unless($jobeet_job = Doctrine::getTable('JobeetJob')->find($request->getParameter('id')), sprintf('Object jobeet_job does not exist (%s).', $request->getParameter('id'))); 
    $this->form = new JobeetJobForm($jobeet_job); 
    } 

    public function executeUpdate(sfWebRequest $request) 
    { 
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put')); 
    $this->forward404Unless($jobeet_job = Doctrine::getTable('JobeetJob')->find($request->getParameter('id')), sprintf('Object jobeet_job does not exist (%s).', $request->getParameter('id'))); 
    $this->form = new JobeetJobForm($jobeet_job); 

    $this->processForm($request, $this->form); 

    $this->setTemplate('edit'); 
    } 

    public function executeDelete(sfWebRequest $request) 
    { 
    $request->checkCSRFProtection(); 

    $this->forward404Unless($jobeet_job = Doctrine::getTable('JobeetJob')->find($request->getParameter('id')), sprintf('Object jobeet_job does not exist (%s).', $request->getParameter('id'))); 
    $jobeet_job->delete(); 

    $this->redirect('job/index'); 
    } 

    protected function processForm(sfWebRequest $request, sfForm $form) 
    { 
    $form->bind($request->getParameter($form->getName())); 
    if ($form->isValid()) 
    { 
     $jobeet_job = $form->save(); 

     $this->redirect('job/edit?id='.$jobeet_job['id']); 
    } 
    } 
} 

在actions.class executeIndex我可以加入其中:

public function executeIndex(sfWebRequest $request) 
    { 
    $this->jobeet_job_list = Doctrine::getTable('JobeetJob') 
     ->createQuery('a') 
     ->where('id = ?', $id) //$id i have in session, this working OK 
     ->execute(); 
    } 

我怎樣才能與executeEdit做出類似?在數據庫中我有字段user_id,它添加了一條消息。我想編輯只能編輯這個消息。感謝幫助!

+0

你有模塊,每個用戶可以添加新聞嗎?用戶可以在前端或後端添加新聞? – denys281

回答

0

如果編輯工作正在進行,您必須找到想要編輯的工作並創建一個用該對象初始化的表單。喜歡的東西(我跳過所有的參數校驗位,顯然,你必須確保該ID是在請求中,用戶登錄和檢索工作存在並且由用戶自備):

$user_id = $this->getUser()->getId(); // if you are logged and getId is defined in your myUser class 

$job_id = $request->getParameter('id', false); 

$job = Doctrine::getTable('JobeetJob')->find($job_id); 

if ($job['owner_id'] == $user_id) 
{ 
    $this->form = new JobeetJobForm($job); 
}