Symfony - 以多页形式传递对象

时间:2022-10-22 18:18:02

I'm trying to implement a multi-form multi-page function. For example I have an Experimentation (entity) which is linked to Data (entity) and Result (entity) with classic foreign key relations. I want to create a entry process like:

我正在尝试实现多形式的多页面功能。例如,我有一个实验(实体),它与经典的外键关系链接到数据(实体)和结果(实体)。我想创建一个输入过程,如:

Experimentation entry (using Experimentation generated form)
Next
Data entry (using Data generated form)
Next
Result entry (using Result generated form)
Validate

The three entities are persisted (and flushed) only at the last step (validate).

这三个实体仅在最后一步(验证)持久化(并刷新)。

I've tried the following code:

我试过以下代码:

/**
 * Creates a new Experimentation entity.
 *
 * @Route("/new", name="experimentation_new")
 * @Method({"GET", "POST"})
 */
public function newAction(Request $request) {

    $experimentation = new Experimentation();
    $form = $this->createForm('AppBundle\Form\ExperimentationType', $experimentation);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $this->experimentation = $experimentation;
        return $this->forward('AppBundle:Experimentation:newData', array(
            'experimentation' => $experimentation
        ));
    }

    return $this->render('experimentation/new.html.twig', array(
            'experimentation' => $experimentation,
            'form' => $form->createView(),
    ));
}

In the same controller:

在同一个控制器中:

/**
 * Creates a new Data for the new Experimentation entity.
 *
 * @Route("/new/data", name="experimentation_new_data")
 * @Method({"GET", "POST"})
 */
public function newDataAction(Request $request, $experimentation = null) {
    $data = new Data();
    $form = $this->createForm('AppBundle\Form\DataType', $data);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // Go to the "Result" entry using the same technique
        return $this->forward('...');
    }

    return $this->render('data/new.html.twig', array(
            'data' => $data,
            'experimentation' => $experimentation,
            'form' => $form->createView(),
    ));
}

It's look like symfony cannot render the second form (the data form based on the Data Entity):

看起来symfony无法渲染第二种形式(基于数据实体的数据形式):

The merge filter only works with arrays or "Traversable", got "NULL" as first argument in data/new.html.twig at line 1.

合并过滤器仅适用于数组或“Traversable”,在第1行的data / new.html.twig中作为第一个参数获得“NULL”。

What I've tried to do:

我试图做的事情:

  • check variables with var_dump: there is NO null variable...
  • 使用var_dump检查变量:没有null变量...
  • replace forward by redirectToRoute: in this case, the form is rendered but I can't manage to "send" the experimentation object from method newAction to method newDataAction.
  • 通过redirectToRoute替换forward:在这种情况下,表单被渲染但我无法将实验对象从方法newAction“发送”到方法newDataAction。

Thank you for your help,

感谢您的帮助,

1 个解决方案

#1


1  

I recently came across this issue and, as the error msg suggests, it was the 'merge' twig filter that was throwing.

我最近遇到了这个问题,并且正如错误消息msg所暗示的那样,正是投掷的'merge'枝条过滤器。

Your new.html.twig template likely extends another template - check that base template for uses of 'merge' (the error message "data/new.html.twig at line 1" is the giveaway, as line 1 will be your 'extends' statement).

你的new.html.twig模板可能会扩展另一个模板 - 检查基本模板是否使用'merge'(第1行的错误消息“data / new.html.twig”是赠品,因为第1行将是你的'扩展'声明)。

In my case, I was doing the following, but app.request.get('_route_params') was null under certain conditions:

在我的情况下,我正在执行以下操作,但app.request.get('_ route_params')在某些条件下为null:

app.request.get('_route_params')|merge({'_locale': 'en'})

#1


1  

I recently came across this issue and, as the error msg suggests, it was the 'merge' twig filter that was throwing.

我最近遇到了这个问题,并且正如错误消息msg所暗示的那样,正是投掷的'merge'枝条过滤器。

Your new.html.twig template likely extends another template - check that base template for uses of 'merge' (the error message "data/new.html.twig at line 1" is the giveaway, as line 1 will be your 'extends' statement).

你的new.html.twig模板可能会扩展另一个模板 - 检查基本模板是否使用'merge'(第1行的错误消息“data / new.html.twig”是赠品,因为第1行将是你的'扩展'声明)。

In my case, I was doing the following, but app.request.get('_route_params') was null under certain conditions:

在我的情况下,我正在执行以下操作,但app.request.get('_ route_params')在某些条件下为null:

app.request.get('_route_params')|merge({'_locale': 'en'})