我在api中有端点发布团队。实体团队有关系实体排名,ManyToMAny和形式,当我创建团队时,我添加排名实体和学说为我做所有的工作 - 冲洗实体。但我有api并使用JMS Serializer。如何使用帮助表单刷新api中的实体团队??? 实体团队
class Teams
{
use Timestampable;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"for_chat", "get_team", "for_sample_set", "for_sample", "for_team_members", "for_profile_project", "for_all_projects"})
*/
protected $id;
/**
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(name="slug", type="string", length=255, nullable=false)
* @Groups({"get_team"})
*/
protected $slug;
/**
* @ORM\ManyToMany(targetEntity="Rank", inversedBy="teams")
* @ORM\JoinTable(name="rank_teams")
* @Groups({"get_team"})
*/
private $rank;
runk
class Rank
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @Groups({"get_team"})
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="Teams", mappedBy="rank")
*/
private $teams;
以形式添加实体runk
$form->add('rank', 'entity', array(
'class' => 'ArtelProfileBundle:Rank',
'property' => 'name',
'expanded' => true,
'multiple' => true,
'required' => false,
));
现在我如何发布实体团队
/**
* Post Team,.
*
* @ApiDoc(
* resource = true,
* description = "Post Team",
* parameters={
* {"name"="image_back", "dataType"="string", "required"=false, "description"="image_back"},
* {"name"="name", "dataType"="string", "required"=true, "description"="name"},
* {"name"="description", "dataType"="string", "required"=false, "description"="description"},
* {"name"="image", "dataType"="string", "required"=false, "description"="image"},
* {"name"="profile_summary", "dataType"="string", "required"=false, "description"="profile_summary"},
* {"name"="web_site", "dataType"="string", "required"=false, "description"="web_site"},
* {"name"="markets", "dataType"="array", "required"=false, "description"="markets"},
* {"name"="skills", "dataType"="array", "required"=false, "description"="skills"},
* {"name"="city", "dataType"="string", "required"=false, "description"="location teams"},
* {"name"="ranks", "dataType"="array", "required"=false, "description"="rank"},
* {"name"="avalible_from", "dataType"="date", "required"=false, "description"="avalible_from like 2013-07-21T01:16:22-0500"},
* {"name"="avalible_to", "dataType"="date", "required"=false, "description"="avalible_to like 2013-07-21T01:16:22-0500"},
* {"name"="token", "dataType"="string", "required"=true, "description"="is equally md5('email'.secret_word)"}
* },
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Secret token is not valid"
* },
* section="Team"
* )
*
* @RestView()
*
* @param Request $request
*
* @return View
*
* @throws NotFoundHttpException when not exist
*/
public function postTeamAction(Request $request)
{
$manager = $this->getDoctrine()->getManager();
$token = $this->get('request')->request->get('token');
$user = $this->getDoctrine()->getRepository('ArtelProfileBundle:Users')->findOneBySecuritytoken($token);
if (!empty($user) && !empty($token)) {
$team = new Teams();
$data = $request->request->all();
$data = $this->get('serializer')->serialize($data, 'json');
$team = $this->get('serializer')->deserialize($data, 'Artel\ProfileBundle\Entity\Teams', 'json');
if (count($errors = $this->get('validator')->validate($team)) > 0) {
$view = $this->view((string) $errors, 400);
return $this->handleView($view);
}
if (!is_null($this->get('request')->request->get('city'))) {
$nameCity = $this->get('request')->request->get('city');
$city = $this->get('artel.profile.additional_function')->addCityAction($nameCity);
$team->setLocation($city);
}
if (!is_null($this->get('request')->request->get('ranks'))) {
$ranks = $this->get('request')->request->get('ranks');
foreach($ranks as $rank){
$rankEntity = $this->get('artel.profile.additional_function')->checkRank($rank);
$team->addRank($rankEntity);
}
}
$manager->persist($team);
$user->setTeams($team);
$manager->flush();
$view = $this->view('Team successful create', 200);
return $this->handleView($view);
} else {
$view = $this->view('Secret token is not valid', 400);
return $this->handleView($view);
}
}
但JMS Serializer不了解ManyToMany排名。对于ManyToOne城市,我这样添加
if (!is_null($this->get('request')->request->get('city'))) {
$nameCity = $this->get('request')->request->get('city');
$city = $this->get('artel.profile.additional_function')->addCityAction($nameCity);
$team->setLocation($city);
}
其中$ city这是实体城市,我检查是否已经返回或创建新的并返回。但这是自定义方法,而不是使用ManyToMany。我创造了类似的东西
if (!is_null($this->get('request')->request->get('ranks'))) {
$ranks = $this->get('request')->request->get('ranks');
foreach($ranks as $rank){
$rankEntity = $this->get('artel.profile.additional_function')->checkRank($rank);
$team->addRank($rankEntity);
}
}
但这种方法并不普遍,这只适用于帖子。如果我使用PUT,那么这些方法不会被删除等级自动像教义与形式。示例我有两个等级,但在编辑形式我选择一个 - doctrine自动删除和自动添加实体runk
如何使用帮助表单来清理具有ManyToMany runk的实体团队,因为表单工作正常