symfony4注入参数,格式为

时间:2019-01-02 21:53:49

标签: php forms symfony twig symfony4

我有一个业务实体和一个BusinessObject实体,当我创建一个新的BusinessObject时,我想将BusinessObject链接到当前业务。 例如,如果我的路线是business/{id}/object/new,那么我想拥有与商家相关的对象(由于ID)。

在BusinessObject Controller中,我设法使用@ParamConverter来获取业务ID。 在我的BusinessObject Form中,我将HiddenType放到我的业务条目中,因为我不希望它出现,并将data设置为business_ID。 我在configureOptions中很难获得业务ID,我不知道如何从此处获取业务ID。

BusinessObject Controller(新路由)

/**
 * @Route("/{post_id}/new", name="business_object_new", methods="GET|POST")
 * @ParamConverter("business", options={"id" = "post_id"})
 */
public function new(Request $request,Business $business): Response
{
    $businessObject = new BusinessObject();

    $businessID = $business->getId();

    $form = $this->createForm(BusinessObjectType::class, $businessObject,array(
        'business_ID'=>$businessID,
    ));
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($businessObject);
        $em->flush();

        return $this->redirectToRoute('business_object_index');
    }

    return $this->render('business_object/new.html.twig', [
        'business_object' => $businessObject,
        'business'=>$business,
        'form' => $form->createView(),
    ]);
}

BusinessObjectType

class BusinessObjectType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
        ->add('object',TextType::class)
        ->add('complement')
        ->add('status')
        ->add('durationExpected')
        ->add('durationAchieved')
        ->add('client')
        ->add('projectManager')
        ->add('business',HiddenType::class,array(
            'data' => $options['business_ID']

        ))
    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => BusinessObject::class,
        'business_ID'=>Business::class
    ]);
}
}

使用此代码,我得到一个错误Expected argument of type "App\Entity\Business or null", "string" given.,我认为这与我的Form中的configureOptions()函数有关

1 个答案:

答案 0 :(得分:1)

方法可以是:

class Product extends Model
{
    public function QuoteItem()
    {
        return $this->hasMany('app\QuoteItem');
    }

}

表单生成器为:

public function new(Request $request,Business $business): Response
{
    $businessObject = new BusinessObject();

    $form = $this->createForm(BusinessObjectType::class, $businessObject);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // I suppose your setter is `setBusiness`, otherwise use more suitable one
        $businessObject->setBusiness($business);
        $em = $this->getDoctrine()->getManager();
        $em->persist($businessObject);
        $em->flush();

另一种选择是将表单{strong>嵌入{strong} builder ->add('object',TextType::class) ->add('complement') ->add('status') ->add('durationExpected') ->add('durationAchieved') ->add('client') ->add('projectManager'); // No business field BusinessType,您可以阅读有关嵌入表单here的更多信息。