我目前正在使用 Symfony2 重建一个网络应用程序。我希望用户更新他们在我们的 MySQL 数据库中的特定记录。但是,用户不是自信的计算机用户,也不喜欢 Web 应用程序(以其当前形式)的工作方式。因此,从 UI/UX 的角度来看,我决定使用 2 个表单来编辑特定数据,而不是当前用户不喜欢的 1 个表单。
有问题的 MySQL 数据库表包含许多信息字段,例如他们的个人详细信息和与他们相关的其他信息。表单被拆分以反映这一点,一个表单更新个人详细信息,一个表单更新其余的,因此用户不必处理一个长表单。
不过,目前,当我使用一种形式时,出现错误:
在表单提交期间,在链配置的命名空间中找不到类“Symfony\Component\Form\Form”
这个错误在这个问题中得到了解决。然而,它给我留下了一个问题。目前,因为我已将表单一分为二,我无法将这些数据保存到数据库中。我可以通过只使用一种形式来克服这个问题,但这违背了数据库用户的意愿。此外,我知道完全可以使用两个或多个表单将特定数据添加到单个数据库,就像我以前做过的那样,只是在 Symfony 中没有。
有谁知道或有一个建议,我可以如何克服这个问题?由于那里存在大量当前数据,因此更改数据库是不可能的。
更新
这是缺少的视图、控制器和表单文件。
view.html.twig
<!-- Modal Windows: Edit Instructor Personal Details -->
<div id="editPersonal" style="display:none;">
<div class="modal-head">
<h2>Edit Personal Details For: <font-color="red !important">{{instructor.firstName}} {{instructor.surname}}</font></h2>
</div>
<div class="modal-body">
<form action="#" method="post" {{ form_enctype(ipde) }} id="editPersonalDetails" class="modaledit">
<table class="modalform-col1">
<tbody>
<tr class="hidden">
<th>{{ form_label(ipde.id, 'ID*', { 'attr': {'class': 'title'} }) }}</th>
<td>
{{ form_errors(ipde.id) }}
{{ form_widget(ipde.id, { 'attr': {'class': 'textfield'}}) }}
</td>
</tr>
<tr>
<th>{{ form_label(ipde.firstName, 'First Name*', { 'attr': {'class': 'title'} }) }}</th>
<td>
{{ form_errors(ipde.firstName) }}
{{ form_widget(ipde.firstName, { 'attr': {'class': 'text'}}) }}
</td>
</tr>
<tr>
<th>{{ form_label(ipde.surname, 'Surname*', { 'attr': {'class': 'title'} }) }}</th>
<td>
{{ form_errors(ipde.surname) }}
{{ form_widget(ipde.surname, { 'attr': {'class': 'text'}}) }}
</td>
</tr>
<tr>
<th>{{ form_label(ipde.address1, 'Address Line 1*', { 'attr': {'class': 'title'} }) }}</th>
<td>
{{ form_errors(ipde.address1) }}
{{ form_widget(ipde.address1, { 'attr': {'class': 'text'}}) }}
</td>
</tr>
<tr>
<th>{{ form_label(ipde.address2, 'Address Line 2', { 'attr': {'class': 'title'} }) }}</th>
<td>
{{ form_errors(ipde.address2) }}
{{ form_widget(ipde.address2, { 'attr': {'class': 'text'}}) }}
</td>
</tr>
<tr>
<th>{{ form_label(ipde.town, 'Town*', { 'attr': {'class': 'title'} }) }}</th>
<td>
{{ form_errors(ipde.town) }}
{{ form_widget(ipde.town, { 'attr': {'class': 'text'}}) }}
</td>
</tr>
<tr>
<th>{{ form_label(ipde.county, 'County*', { 'attr': {'class': 'title'} }) }}</th>
<td>
{{ form_errors(ipde.county) }}
{{ form_widget(ipde.county, { 'attr': {'class': 'text'}}) }}
</td>
</tr>
<tr>
<th>{{ form_label(ipde.postcode, 'Postcode*', { 'attr': {'class': 'title'} }) }}</th>
<td>
{{ form_errors(ipde.postcode) }}
{{ form_widget(ipde.postcode, { 'attr': {'class': 'text'}}) }}
</td>
</tr>
<tr>
<th>{{ form_label(ipde.email, 'Email*', { 'attr': {'class': 'title'} }) }}</th>
<td>
{{ form_errors(ipde.email) }}
{{ form_widget(ipde.email, { 'attr': {'class': 'text'}}) }}
</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<div class="modal-placeright">
<a href="#close" rel="modal:close" class="closebutton">Close Without Saving</a>
<input type="submit" value="Save Changes" id="savebuttonpr" class="savebutton" />
{{ form_rest(ipde) }}
</div>
</div>
</div>
DefaultController.php
<?php
namespace PCUK\InstructorBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use PCUK\InstructorBundle\Form\IpdeType;
use PCUK\InstructorBundle\Form\IrType;
use PCUK\InstructorBundle\Form\BaType;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function viewAction($instructor, Request $request)
{
// Database connection
$insrep = $this->getDoctrine()->getManager();
// Get Instructor from Entity for Form use
$instructorQ = $insrep->getRepository('InstructorBundle:MapInstructors')->find($instructor);
// Get Shared Branches from Entity for Form use
$instructorS = $insrep->getRepository('InstructorBundle:MapInstructorShared')->find($instructor);
// Generate Form to edit Instructor Personal Details
$ipde = $this->createForm( new IpdeType(), $instructorQ);
// Handle Form submission to edit Instructor Personal Details
if ($request->getMethod() == 'POST') {
$ipde->bind($request);
if ($ipde->isValid()) {
// perform some action, such as saving the task to the database
//if ($this->request->isXmlHttpRequest()){
//return data ajax requires.
//}
$em = $this->getDoctrine()->getManager();
$em->persist($ipde);
$em->flush();
return $this->redirect($this->generateUrl('task_success'));
}
}
// Generate Form to edit Instructor Records
$ir = $this->createForm( new IrType(), $instructorQ);
// Generate Form to edit Instructor Records
$ba = $this->createForm( new BaType(), $instructorS);
// Return data to view
return $this->render('InstructorBundle:Default:view.html.twig', array(
'ipde' => $ipde->createView(),
'ir' => $ir->createView(),
'ba' => $ba->createView()
));
}
}
IpdeType.php - 个人资料表格
<?php
// src/PCUK/InstructorBundle/Form/Type/IpdeType.php
// This is to handle forms for the Instructor Personal Details Form
namespace PCUK\InstructorBundle\Form;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FileField;
class IpdeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', 'integer', array('required'=>false));
//Personal Details
$builder->add('firstName', 'text', array('required'=>false));
$builder->add('surname', 'text', array('required'=>false));
$builder->add('address1', 'text', array('required'=>false));
$builder->add('address2', 'text', array('required'=>false));
$builder->add('town', 'text', array('required'=>false));
$builder->add('county', 'text', array('required'=>false));
$builder->add('postcode', 'text', array('required'=>false));
$builder->add('email', 'text', array('required'=>false));
$builder->add('phone', 'text', array('required'=>false));
$builder->add('mobile', 'text', array('required'=>false));
$builder->add('notes', 'text', array('required'=>false));
}
public function getName()
{
return 'ipde';
}
}
IrType.php - 其他信息表
<?php
// src/PCUK/InstructorBundle/Form/Type/IrType.php
// This is to handle forms for the Instructor Records Form
namespace PCUK\InstructorBundle\Form;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FileField;
class IrType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', 'integer', array('required'=>false));
$builder->add('primaryArea', 'integer', array('required'=>false));
$builder->add('primaryBranch','entity', array('class'=>'PCUK\InstructorBundle\Entity\MapBranches', 'property'=>'branchname' ));
$builder->add('begin', 'date', array('required'=>false));
$builder->add('lastCrb', 'date', array('required'=>false));
$builder->add('latestCpd', 'date', array('required'=>false));
$builder->add('preferredLevel','entity', array('class'=>'PCUK\InstructorBundle\Entity\MapInstructorLevels', 'property'=>'name' ));
$builder->add('preferredDiscipline','entity', array('class'=>'PCUK\InstructorBundle\Entity\MapInstructorLevels', 'property'=>'name' ));
$builder->add('currentLevel','entity', array('class'=>'PCUK\InstructorBundle\Entity\MapInstructorLevels', 'property'=>'name' ));
$builder->add('bhs', 'checkbox', array('required'=>false));
$builder->add('visiting','entity', array('class'=>'PCUK\InstructorBundle\Entity\MapInstructorVis', 'property'=>'name' ));
}
public function getName()
{
return 'ir';
}
}
更新:29/04/13 按照 james_t 的建议,我将实体一分为二,每个表格各一个。但是,原始错误仍然存在。
我还在控制器中创建了一个新操作,并且由于拆分实体没有解决问题,我恢复使用单个实体。我的控制器现在如下所示:
viewAction public function viewAction($instructor, Request $request) { // 数据库连接 $insrep = $this->getDoctrine()->getManager();
// Get IPDE from Entity for Form use
$instructorIpde = $insrep->getRepository('InstructorBundle:MapInstructors')->find($instructor);
// Generate Form to edit Instructor Personal Details
$ipde = $this->createForm( new IpdeType(), $instructorIpde);
// Get IR from Entity for Form use
$instructorIr = $insrep->getRepository('InstructorBundle:MapInstructors')->find($instructor);
// Generate Form to edit Instructor Records
$ir = $this->createForm( new IrType(), $instructorIr);
// Get Shared Branches from Entity for Form use
$instructorBa = $insrep->getRepository('InstructorBundle:MapInstructorShared')->find($instructor);
// Generate Form to edit Instructor Records
$ba = $this->createForm( new BaType(), $instructorBa);
// Return data to view
return $this->render('InstructorBundle:Default:view.html.twig', array(
'pagename' => $iname . ' - Instructors',
'ipde' => $ipde->createView(),
'ir' => $ir->createView(),
'ba' => $ba->createView(),
'iid' => $instructor
));
}
ipdeAction
public function ipdeAction($instructor, Request $request) { // 数据库连接 $insrep = $this->getDoctrine()->getManager();
// Get IPDE from Entity for Form use
$instructorIpde = $insrep->getRepository('InstructorBundle:MapInstructors')->find($instructor);
// Generate Form to edit Instructor Personal Details
$ipde = $this->createForm( new IpdeType(), $instructorIpde);
// Handle Form submission to edit Instructor Personal Details
if ($request->getMethod() == 'POST') {
$ipde->bind($request);
if ($ipde->isValid()) {
// perform some action, such as saving the task to the database
//if ($this->request->isXmlHttpRequest()){
//return data ajax requires.
//}
$em = $this->getDoctrine()->getManager();
$em->persist($ipde);
$em->flush();
$params = array(
'instructor' => $instructor,
);
return $this->redirect($this->generateUrl('instructor_viewinstructor', $params));
}
}
}
我还从这里修复了更新我的view.html.twig文件:
<form action="#" method="post" {{ form_enctype(ipde) }} id="editPersonalDetails" class="modaledit">
对此:
<form action="{{ path('instructor_viewinstructor_ipde', {'instructor' :iid}) }}" method="post" {{ form_enctype(ipde) }} id="editPersonalDetails" class="modaledit">
我的 routing.yml 文件现在也看起来像这样:
instructor_viewinstructor:
pattern: /instructors/view/{instructor}
defaults: { _controller: InstructorBundle:Default:view }
instructor_viewinstructor_ipde:
pattern: /instructors/view/{instructor}/ipde
defaults: { _controller: InstructorBundle:Default:ipde }
requirements:
_method: POST