我在询问之前经常搜索(永远不够)所以我希望这不是一个重复的问题。
好吧,我走了:
我正在使用symfony开发REST API(使用FOSRestBundle和JMSSerializerBundle)。我尝试使用以下正文进行POST查询:
{
"name":"Planta 1",
"parentJoint": null,
"owner": 1,
"ownerCRUD":{
"C":"0",
"R":"0",
"U":"0",
"D":"0"
}
填写以下Doctrine的实体
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Joint
*
* @ORM\Table(name="joint")
* @ORM\Entity(repositoryClass="AppBundle\Repository\JointRepository")
*/
class Joint
{
(...)
/**
* @var array
*
* @ORM\Column(name="owner_crud", type="array",nullable = false)
*/
private $ownerCRUD;
/**
* Set ownerCRUD
*
* @param array $ownerCRUD
* @return Joint
*/
public function setOwnerCRUD($ownerCRUD)
{
$this->ownerCRUD = $ownerCRUD;
return $this;
}
/**
* Get ownerCRUD
*
* @return array
*/
public function getOwnerCRUD()
{
return $this->ownerCRUD;
}
public function __toString()
{
return 'any string';
}
}
我尝试通过此控制器的功能:
/**
* Description: This method create and persist in database a new Joint Element
*
* @ApiDoc(
* )
*
* @var Request $request
*/
public function postJointAction(Request $request)
{
try {
$parameters = $request->request->all();
dump($parameters);
$joint = new Joint();
$form = $this->createForm('AppBundle\Form\JointType', $joint);
$form->submit($parameters, 'POST');
dump($form);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$joint = $form->getData();
$em->persist($joint);
$em->flush($joint);
$joint = $em->getRepository('AppBundle:Joint')->find($joint->getId());
//Create a View
$templateData = array('joint' => $joint);
$view = $this->view($templateData,200)
->setTemplate("joint/show.html.twig")
->setTemplateVar('joint')
->setTemplateData($templateData)
->setData($templateData)
;
return $this->handleView($view);
}
} catch (InvalidFormException $exception) {
return $exception->getForm();
}
}
我的表格是这样的:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class JointType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('parentJoint')
->add('owner')
->add('ownerCRUD') ;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Joint',
'csrf_protection' => false
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_joint';
}
/**
* @return string
*/
public function getName()
{
return "";
}
}
问题是表单没有正确获取ownerCRUD字段,表单假设是额外的字段而不是正确的字段
This form should not contain extra fields. ownerCRUD
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[ownerCRUD] = [C => 0, R => 0, U => 0, D => 0]
提前致谢
我的枝条文件
{% extends 'base.html.twig' %}
{% block body %}
<h1>Joint</h1>
<table>
<tbody>
<tr>
<th>Id</th>
<td>{{ joint.id }}</td>
</tr>
<tr>
<th>Name</th>
<td>{{ joint.name }}</td>
</tr>
</tbody>
</table>
<ul>
<li>
<a href="{{ path('get_joints') }}">Back to the list</a>
</li>
<li>
<a href="{{ path('put_joint', { 'joint': joint.id }) }}">Edit</a>
</li>
</ul>
{% endblock %}
答案 0 :(得分:1)
你的身体对我来说似乎无效。由于ownerCRUD应该是一个数组,你的身体应该是这样的:
{
"name":"Planta 1",
"parentJoint": null,
"owner": 1,
"ownerCRUD":[
"0",
"0",
"0",
"0"
]
}
注意[]而不是{}围绕&#34; C&#34;:&#34; 0&#34;,&#34; R&#34;:&#34; 0&#34;,&# 34; U&#34;:&#34; 0&#34;,&#34; D&#34;:&#34; 0&#34;
修改强>
删除了密钥&#34; C&#34;,&#34; R&#34;,&#34; U&#34;,&#34; D&#34;
答案 1 :(得分:0)
我建议你改变你的实体..
我的替代方案:
将“ownerCRUD”的数据类型转换为字符串值..
联合实体:
/**
* @var array
*
* @ORM\Column(name="owner_crud", type="string",nullable = false)
*/
private $ownerCRUD;
/**
* Set ownerCRUD
*
* @param array $ownerCRUD
* @return Joint
*/
public function setOwnerCRUD(array $ownerCRUD)
{
$this->ownerCRUD = $ownerCRUD;
return $this;
}
/**
* Get ownerCRUD
*
* @return array
*/
public function getOwnerCRUD()
{
return array($this->ownerCRUD);
}