0

我用 Jobeet 学习 Symfony 1.4。我为用户做了 Jobeet 和系统登录。现在我想添加可能性编辑自己的附属公司。

<?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 我可以添加 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();
  }

我怎样才能用executeEdit做类似的事情?在数据库中,我有字段 user_id,它添加了一条新闻。我想编辑只能作者这个新闻。感谢帮助!

4

1 回答 1

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);
}
于 2011-08-06T17:14:37.867 回答