代码之家  ›  专栏  ›  技术社区  ›  manu

REST API:如何发布与fosrestbundle的多人关系

  •  2
  • manu  · 技术社区  · 8 年前

    在我的Symfony 4项目中,我有两个实体:问题和可能答案。
    一个问题可以有许多可能的答案,一个答案可以用于许多不同的问题。

    我想发布一个问题,我使用 FOSRestBundle .
    以下是我将如何格式化请求正文:

    {
        "title": "my question",
        "possibleAnswers": [
            {
                "id": 1,
                "title": "my first answer"
            }
        ]
    }
    

    至少,当我直接在数据库中创建数据并得到问题时,FOSRestBundle就是这样格式化答案的。

    但我一直在犯同样的错误:

    验证失败,可能回答此值无效

    我使用Symfony表单验证我的帖子,并且我用于可能回答问题的字段的字段类型是EntityType,选项multiple设置为true。也许这就是错误的来源。

    所以我的问题是:
    如何设置请求正文的格式以添加有效的possibleAnswers字段?如果我当时做对了,我应该使用哪种表单类型来工作?

    很抱歉没有添加任何代码,上下文实际上相当复杂,但我很确定我字段的JSON格式或表单类型是错误的。

    [编辑]

    下面是我为possibleanswers字段尝试的所有表单类型

    //->add('possibleAnswers', EntityType::class, array('data' => [], 'required' => false, 'multiple' => true, 'class' => PossibleAnswer::class))
      ->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => EntityType::class, 'entry_options' => array('class' => PossibleAnswer::class)))
    //->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => PossibleAnswerType::class))
    //->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => PossibleAnswerType::class, 'entry_options' => array('class' => PossibleAnswer::class)))
    

    以下是我尝试选择的所有JSON格式:

    {
        "title": "a postman created question",
        "answerConditions": [
            {
                "id": 1,
                "title": "already existing question",
            }
        ]
    }
    
    {
        "title": "a postman created question",
        "answerConditions": [
            {
                "possibleanswer_id": 1
            }
        ]
    }
    
    {
        "title": "a postman created question",
        "answerConditions": [
            {
                "id": 1
            }
        ]
    }
    
    {
        "title": "a postman created question",
        "answerConditions": [
            1
        ]
    }
    
    1 回复  |  直到 8 年前
        1
  •  4
  •   Antoine Galluet    8 年前

    如果要在发布问题时创建可能的答案,需要在表单中用CollectionType替换EntityType。

    当我想在发布父对象(问题)时发布子对象(这里是可能的答案)时,我经常在发布请求中使用它:

    问题表格

    $builder
        ->add('possibleAnswers', CollectionType::class, array(
            'entry_type'  => PossibleAnswerType::class,
            'allow_add' => true,
        ))
    ;
    

    可能的答案形式:

    $builder
        ->add('title')
    ;
    

    json看起来像:

    {
        "title": "my question",
        "possibleAnswers": [
            {
                "title": "my first answer"
            }
        ]
    }
    

    希望能有所帮助。

    编辑: 如果要在创建问题时将可能的答案链接到问题,可以使用以下选项:

    问题形式:

    $builder
        ->add('possibleAnswer', EntityType::class, array(
            'class' => YOUR_ENTITY::class,
            'multiple' => true
        ));
    

    json应该只包含要链接到的实体的id。应该是这样的:

    {
        "title": "a postman created question",
        "answerConditions": [
            1
        ]
    }