如何在Symfony 1.4(Propel)中编辑现有记录

时间:2015-03-19 15:20:44

标签: php forms orm symfony-1.4 propel

我有表格行我希望用户能够通过使用其记录值(表名:Vendor)填充表单进行编辑,然后提交已编辑的数据。

我已阅读并重新阅读Symfony 1.4文档,但我遇到了麻烦: 1.)使用现有记录数据填充表单字段。 2.)保存已编辑的表格。

下面的表单是用于创建供应商的表单,据我所知,编辑现有记录是可以接受的。

对于问题#1,我尝试将现有记录值(在__construct()操作中)带到表单,然后使用$this->setDefaults将其设置为默认值 < / strong>在编辑操作中重定向到表单时填充表单输入但是当用户尝试重定向到create action中的表单时,表单不起作用,这表示默认找不到值。

同样值得注意的是,该模块中有多种形式。使用表单创建记录没有问题,只有在创建后才编辑它们。

任何方向或建议?

以下代码。

控制器:

public function executeVendorEdit(sfWebRequest $request)
{
    $ind_vendor = VendorPeer::retrieveByPK(($request->getParameter('id')));
    $this->form = new VendorCreateForm(array('name' => $ind_vendor));

if ($request->isMethod('post')) {

    $this->form->bind($request->getParameter('vendor'));
    if ($this->form->isValid()) {
        $vendor = $this->form->save();

        $this->redirect('catalog/vendorEdit?id=' . $vendor->getId());
        }
}
$this->setTemplate('vendorEdit');
}

表格

class VendorCreateForm extends SmartForm
{
    protected $is_authenticated = null;
    public function __construct($is_authenticated = false)
    {
        $this->is_authenticated = $is_authenticated;

        parent::__construct();
    }

    public function setup()
    {
        $this->setWidgets(array(
            'name' => new sfWidgetFormInput(array(),
                array('class' => 'form-control')),
            'contact_name' => new sfWidgetFormInput(array(),
                array('class' => 'form-control')),
            'contact_email' => new sfWidgetFormInput(array(),
                array('class' => 'form-control')),
            'contact_phone' => new sfWidgetFormInput(array(),
                array('class' => 'form-control')),
            'address1' => new sfWidgetFormInput(array(),
                array('class' => 'form-control')),
        ));

        $this->setValidators(array(
            'name' => new sfValidatorString(array('required' => true)),
            'contact_name' => new sfValidatorString(array('required' => true)),
            'contact_email'   => new sfValidatorEmail(array('required' => true)),
            'contact_phone' => new sfValidatorString(array('required' => true)),
            'address1' => new sfValidatorString(array('required' => true)),
       ));

        $this->widgetSchema->setNameFormat('vendor[%s]');

        $this->setDefaults(array(
        ));

        parent::setup();
    }
}

错误:

  

致命错误:在第72行的/projects/fun-project/src/apps/operations/modules/catalog/actions/actions.class.php中调用未定义的方法VendorCreateForm :: save()

1 个答案:

答案 0 :(得分:0)

经过很多时间和挫折,我解决了这两个问题。使用值填充表单将在表单中进行。它需要将供应商值传递给构造操作中的表单,然后在存在供应商值的情况下在设置操作中设置默认值:if ($this->vendor)

注意:$is_authenticated声明和构造不是必需的(查看我在问题中包含的表格)。

<强>形式:

class VendorCreateForm extends SmartForm
{

protected $vendor = null;

public function __construct($vendor = false)
{
    $this->vendor = $vendor;

    parent::__construct();
}

public function setup()
{
    $this->setWidgets(array(
        'name' => new sfWidgetFormInput(array(),
            array('class' => 'form-control')),
        'contact_name' => new sfWidgetFormInput(array(),
            array('class' => 'form-control')),
        'contact_email' => new sfWidgetFormInput(array(),
            array('class' => 'form-control')),
        'contact_phone' => new sfWidgetFormInput(array(),
            array('class' => 'form-control')),
        'address1' => new sfWidgetFormInput(array(),
            array('class' => 'form-control')),
    ));

    $this->setValidators(array(
        'name' => new sfValidatorString(array('required' => true)),
        'contact_name' => new sfValidatorString(array('required' => true)),
        'contact_email'   => new sfValidatorEmail(array('required' => true)),
        'contact_phone' => new sfValidatorString(array('required' => true)),
        'address1' => new sfValidatorString(array('required' => true)),
   ));

    $this->widgetSchema->setNameFormat('vendor[%s]');

    if ($this->vendor)
    {
        $this->setDefaults(array(
        'name' => $this->vendor->getName(),
        'contact_name' => $this->vendor->getContactName(),
        'contact_email' => $this->vendor->getContactEmail(),
        'contact_phone' => $this->vendor->getContactPhone(),
        'address1' => $this->vendor->getAddress1(),
        ));
    }

    parent::setup();

}

正确保存已编辑的表单在控制器中进行。正确的executeVendorEdit操作与我的executeVendorCreate操作几乎完全相同,但有两点不同:1。)通过Propel Query $this->vendor = VendorPeer::retrieveByPK($request->GetParameter('id'))检索当前对象和2.) NOT 声明一个新的模型对象,但是而是将值保存到当前对象$this->vendor。代码如下。

<强>控制器

public function executeVendorEdit(sfWebRequest $request)
{
    $this->vendor = VendorPeer::retrieveByPK($request->GetParameter('id'));

    $this->form = new VendorCreateForm($this->vendor);

    if ($request->isMethod('post')) {

        $this->form->bind($request->getParameter('vendor'));
        if ($this->form->isValid())
        {
            $values = $this->form->getValues();

            $this->vendor->setName($values['name']);
            $this->vendor->setContactName($values['contact_name']);
            $this->vendor->setContactEmail($values['contact_email']);
            $this->vendor->setContactPhone($values['contact_phone']);
            $this->vendor->setAddress1($values['address1']);
            $this->vendor->setCreatedTs('now');
            $this->vendor->save();

            $this->redirect('catalog/vendors');
        }
    }
}