开发者

action.class for current user

i learn Symfony 1.4 with Jobeet. I made Jobeet and system login for user. Now i would like add possibility edit own affiliate.

<?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->che开发者_C百科ckCSRFProtection();

    $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']);
    }
  }
}

In actions.class executeIndex i can add where:

  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();
  }

how can i make similarly with executeEdit? in database i have field user_id, which added a news. I would like to edit can only author this news. thanks for help!


If its editing the job what you are trying to do, you have to find the job you want to edit and create a form initialized with the object. Something like (i'm skipping all the parameter checking bits, obviously you have to make sure that the id is in the request, the user is logged and the job retrieved exists and is owned by the user):

$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);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜