500内部服务器错误 - 传递的参数必须是类类型,但整数给 - symfony2

时间:2017-03-09 18:27:16

标签: php json symfony

我在symfony2应用程序中创建了两个类之间的关系。它的设计使得tableB与tableA相关,我在tableA表中输入数据(作为超级实体),在我的控制器中,我使用json来发布一个帖子,将数据插入tableB表。来自tableB的引用外键需要一个整数但是当我尝试从邮递员发帖时我得到了这个奇怪的错误

{
"message": "Expected value of type "\myBundle\Entity\TableA" for association field "\myBundle\Entity\Platforms#$dataValue", got "integer" instead.",
"class": "Doctrine\ORM\ORMInvalidArgumentException",

这是我从邮递员发布的json格式

 {
"id": 6,
"variable": false,
"variable": true,
"variable": true,
"variable": true,
"variable": false,
"variable": true,
"variable": true,
"variable": true,
"variable": false,
"variable": false,
"variable": true,
"fkValue":3 //area of challenge
}

在我的实体类中,我有这段代码

/**
     * 
     *
     * @param \MyBundle\Entity\TableA $dataValue 
     * @return Platforms
     */
    public function setValue(\Api3Bundle\Entity\TableA $dataValue  = null)
    {
        $this->dataValue  = $dataValue ;

        return $this;
    }

===== EDITED ==================

这就是我将控制器中的数据插入TableB到TableA的外键字段

的方法
//getting the value from request
$variable = $request->get('variable');

//setting the value for persistence
$data->setValue($variable);//area of challenge
  $em = $this->getDoctrine()->getManager();
$em = $this->getDoctrine()->getManager();
  $em->persist($data);

我不知道我是否在正确的轨道上,但有些人可以告诉我如何使用邮递员的json将数据插入外键字段。非常感谢

1 个答案:

答案 0 :(得分:0)

由于您只发送密钥,因此可以使用:

$data->setValue($this->entityManager->getReference('EntityName', $request->get('fkValue')));//area of challenge

来源:http://doctrine-orm.readthedocs.io/en/latest/reference/unitofwork.html