gpt4 book ai didi

php - 在 Zend Framework 中处理记录表单的最佳方式

转载 作者:搜寻专家 更新时间:2023-10-31 20:56:27 25 4
gpt4 key购买 nike

一旦您对根据教程中的示例构建的基本记录表单感到满意,您就会意识到您需要更专业设计的记录表单。例如。我不想在用户和管理区域中为同一个表复制记录表。

1) 是否有人使用某种机制(可能是继承)来减少几乎相似的管理和用户表单的重复?这是否很繁琐,或者有时您最好只使用复制粘贴?

2) 有没有人认为构建一些基本的 Record 类是个好主意

  • 可以确定在本页的几个记录表中,当前帖子是专门针对这个记录表的
  • 能够以某种有条理的方式区分“编辑”或“删除”按钮的点击。

3) 我目前的做法包括将所有表单配置代码(装饰器、验证、初始值)放入构造函数中,并将表单提交处理放入单独的 ProcessSubmit() 方法中,以释放不必要代码的 Controller 。

以上所有地址都针对一些预期的记录表单功能,我想知道是否有任何指南、好的示例应用程序可以用于这种稍微更高级的记录处理,或者人们仍在重新发明轮子。想知道您应该走多远以及应该在何处停止此类改进...

最佳答案

一些建议:

首先 - 在对表单进行子类化时,使用 init() 函数而不是构造函数来添加元素。 init() 函数在设置传递给类的参数之后发生。

第二 - 除了子类化你的表单 - 你可以设置一个“选项”来启用管理功能:

class My_Record_Form extends Zend_Form {
protected $_record = null;
public function setRecord($record) {
$this->_record = $record;
}

public function getRecord() {
if ($this->_record === null || (!$this->_record instanceOf My_Record)) {
throw new Exception("Record not set - or not the right type");
}
return $this->_record;
}

protected $_admin = false;
public function setAdmin($admin) {
$this->_admin = $admin;
}

public function getAdmin() { return $this->_admin; }

public function init() {
$record = $this->getRecord();

$this->addElement(......);
$this->addElement(......);
$this->addElement(......);

if ($this->getAdmin()) {
$this->addElement(.....);
}

$this->setDefaults($record->toArray());
}

public function process(array $data) {
if ($this->isValid($data)) {
$record = $this->getRecord();
if (isset($this->delete) && $this->delete->getValue()) {
// delete button was clicked
$record->delete();
return true;
}
$record->setFromArray($this->getValues());
$record->save();
return true;
}
}
}

然后在你的 Controller 中你可以做这样的事情:

$form = new My_Record_Form(array(
'record'=>$record,
'admin'=>My_Auth::getInstance()->hasPermission($record, 'admin')
));

创建一个也处理管理事务的 My_Record_Admin_Form 并没有什么“错误”——但我发现这种方法将所有“记录表单”代码保存在一个地方,并且更容易维护。

回答第 2 部分:我的代码中的编辑表单是从模型的函数返回的:$record->getEditForm() Controller 代码最终看起来有点像这样:

  protected $_domain = null;
protected function _getDomain($allowNew = false)
{
if ($this->_domain)
{
return $this->view->domain = $this->_domain;
} else {
$id = $this->_request->getParam('id');
if (($id == 'new' || $id=='') && $allowNew)
{
MW_Auth::getInstance()->requirePrivilege($this->_table, 'create');
$domain = $this->_table->createRow();
} else {
$domain = $this->_table->find($id)->current();
if (!$domain) throw new MW_Controller_404Exception('Domain not found');
}
return $this->view->domain = $this->_domain = $domain;
}
}

public function editAction()
{
$domain = $this->_getDomain(true);

MW_Auth::getInstance()->requirePrivilege($domain,'edit');
$form = $domain->getEditForm();

if ($this->_request->isPost() && $form->process($this->_request->getPost()))
{
if ($form->delete && $form->delete->getValue())
{
return $this->_redirect($this->view->url(array(
'controller'=>'domain',
'action'=>'index',
), null, true));
} else {
return $this->_redirect($this->view->url(array(
'controller'=>'domain',
'action'=>'view',
'id'=>$form->getDomain()->id,
), null, true));
}
}
$this->view->form = $form;
}

因此 - 例如,记录的实际 ID 在 URI/domain/edit/id/10 中传递。如果您要在一个页面上放置多个这样的表单 - 您应该确保将表单的“action”属性设置为指向特定于该表单的操作。

关于php - 在 Zend Framework 中处理记录表单的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1223189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com