渲染和更新具有相关记录的 Symfony 表单?

发布于 2024-11-02 00:49:29 字数 2154 浏览 0 评论 0原文

我有两张桌子。我正在渲染两个表的字段(useruser_education)。一个用户可以拥有多条记录(教育详细信息)。要编辑用户信息,我只能检索特定 user_education 详细信息的一条记录。如何检索特定用户的 user_education 表的多条记录?我在名为 editSuccess() 的单个页面中渲染两个表中的字段。

我创建了两种形式的对象,如下所示

foreach ($user->getUserEducations() as $edu)
{
     $education =  $edu;
     break;
}


$this->form = new stFormProfile($user);
$this->form1 = new UserEducationForm($education);

在上面的代码中 $educationuser_education 的单个记录。我应该传递一个 user_education 对象数组( $user->getUserEducations() )。但它不接受对象数组。

我正在绑定值,如下所示,

$this->form1->bind($request->getParameter('user_education'),
$request->getFiles('user_education'));
$this->form->bind($request->getParameter('profile'), $request->getFiles('profile'));

我使用下面的代码保存更新的值,

$this->form->save();
$this->form1->save();

用户表的字段正在更新。但不适用于 user_education 表。我得到了 _csrf_token [必需。] 错误。

UserEducationForm.class.php 是由 Symfony 本身使用我最初给出的数据库模式创建的。在该架构中,我没有为 user_education 表指定任何主键。因此,Symfony 自己创建了一个名为 id 的列,并带有主键。这有什么影响吗?

我正在渲染这些字段,如下所示

print $form["username"]->renderLabel()
print $form["username"]->render() 
print $form["email"]->renderLabel() 
print $form["email"]->render() 
print $form["firstname"]->renderLabel() 
print $form["firstname"]->render() 
print $form["lastname"]->renderLabel() 
print $form["lastname"]->render() 
print $form['skillset']->renderLabel() 
print $form['skillset']->render() 
print $form['years_of_experience']->renderLabel() 
print $form['years_of_experience']->render() 

教育详细信息

print $form1['college_name']->renderLabel() 
print $form1['college_name']->render() 
print $form1['coursename']->renderLabel() 
print $form1['coursename']->render() 
print $form1['percentage']->renderLabel()  

我需要渲染多行教育详细信息表(使用 foreach)。

I have two tables. I'm rendering fields of two tables (user and user_education). One user can have multiple records (educations details). To edit an user's information, I'm able to retrieve only one record of particular user_education's details . How do I retrieve multiple records of the user_education table for a particular user? I'm rendering fields from both tables in single page called editSuccess().

I've created object of both forms, as shown below

foreach ($user->getUserEducations() as $edu)
{
     $education =  $edu;
     break;
}


$this->form = new stFormProfile($user);
$this->form1 = new UserEducationForm($education);

In above code $education is a single record of user_education. I should pass an array of user_education objects ( $user->getUserEducations() ). But it won't accept a array of objects.

I'm binding values as shown below,

$this->form1->bind($request->getParameter('user_education'),
$request->getFiles('user_education'));
$this->form->bind($request->getParameter('profile'), $request->getFiles('profile'));

I'm saving the updated values using the below code,

$this->form->save();
$this->form1->save();

User table's fields are getting updated. But not for user_education table. I'm getting
_csrf_token [Required.] error.

And UserEducationForm.class.php is created by Symfony itself using the database schema which I had given initially . In that schema I didn't specify any primary keys for the user_education table. So Symfony itself created a column called id with the primary key. Is this affecting anything?

I'm rendering those fields as below

print $form["username"]->renderLabel()
print $form["username"]->render() 
print $form["email"]->renderLabel() 
print $form["email"]->render() 
print $form["firstname"]->renderLabel() 
print $form["firstname"]->render() 
print $form["lastname"]->renderLabel() 
print $form["lastname"]->render() 
print $form['skillset']->renderLabel() 
print $form['skillset']->render() 
print $form['years_of_experience']->renderLabel() 
print $form['years_of_experience']->render() 

Education Details

print $form1['college_name']->renderLabel() 
print $form1['college_name']->render() 
print $form1['coursename']->renderLabel() 
print $form1['coursename']->render() 
print $form1['percentage']->renderLabel()  

I need to render multiple rows of Education details table (using foreach).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2024-11-09 00:49:29

您还需要渲染 _csrf_token 字段

print $form['_csrf_token'] 

print $form1['_csrf_token'] 

You need to render the _csrf_token field aswell

print $form['_csrf_token'] 

print $form1['_csrf_token'] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文