I'm working on FOSRestBundle to create new place entity but I have problem with data Transformers, my placeType code is:
<?php
namespace Acme\PlacesBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use FOS\RestBundle\Form\Transformer\EntityToIdObjectTransformer;
class PlacesType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$em = $options['em'];
$cityTransformer = new EntityToIdObjectTransformer($em ,'AcmePlacesBundle:CityDB' );
$typesTransformer = new ManyToEntityTransformer($em, 'AcmePlacesBundle:Type' );
$builder
->add('name','text',array('label' => 'form.name'))
->add('vicinity','text',array('label' => 'form.vicinity'))
->add($builder->create('city', 'text')->addModelTransformer($cityTransformer))
->add('lat','number',array('label' => 'form.lat'))
->add('lng','number',array('label' => 'form.lng'))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\PlacesBundle\Entity\Places',
'csrf_protection' => false,
'validation_groups' => array('info', 'Default'),
'translation_domain' => 'PlacesBundle',
'em' => null,
))
->setRequired(array('em'))
->setAllowedTypes('em', 'Doctrine\Common\Persistence\ObjectManager');
}
/**
* @return string
*/
public function getName()
{
return 'new_place';
}
}
and my places Entity Class:
<?php
namespace Acme\PlacesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Places
*
* @ORM\Table(name="places")
* @ORM\Entity(repositoryClass="\Acme\PlacesBundle\Entity\PlacesRepository")
* @ORM\HasLifecycleCallbacks
*/
class Places
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=70 ,unique=false, nullable=false)
*
*/
protected $name;
/**
* @var string $vicinity
*
* @ORM\Column(name="vicinity", type="string", length=100 ,unique=false, nullable=false)
*
*/
protected $vicinity;
/**
* @var float $lat
*
* @ORM\Column(name="lat", type="float" ,unique=false, nullable=false)
*
*/
protected $lat;
/**
* @var float $lng
*
* @ORM\Column(name="lng", type="float" ,unique=false, nullable=false)
*
*/
protected $lng;
/**
* @ORM\ManyToOne(targetEntity="CityDB")
* @ORM\JoinColumn(name="city_id", referencedColumnName="id")
**/
protected $city;
/**
* @ORM\ManyToMany(targetEntity="Type")
* @ORM\JoinTable(name="places_types",
* joinColumns={@ORM\JoinColumn(name="place_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="type_id", referencedColumnName="id")}
* )
**/
protected $types;
and my PlacesConroller Class :
<?php
namespace Acme\PlacesBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Acme\PlacesBundle\Entity\Places;
use FOS\RestBundle\View\View;
/**
* Description of PlacesAPI
*
* @author Slaiman
*/
class PlacesController extends FOSRestController
{
public function postPlacesAction(Request $request)
{
$place = new Places();
$form = $this->createForm('new_place', $place, array('em' => $this->getDoctrine()->getManager()));
$form->handleRequest($request);
if ($form->isValid()) {
$view = View::create($form->getData(),200);
} else {
$view = View::create($form, 400);
}
return $this->handleView($view);
}
}
my problem is when I send Post request like
{"new_place": {"name":"Solomon","vicinity":"somewhere", "city":{"id":1, "name":"syria"},"lat":33.33,"lng":33.33}}
to postPlacesAction I get the error
{message: "Entity was not found.", class: "Doctrine\\ORM\\EntityNotFoundException",…} class: "Doctrine\\ORM\\EntityNotFoundException" message: "Entity was not found."
.Is there a better approach to work with entity form fields in FOSRestBundle and i Have Types field as multiple entity for place how can i work with it in RestBundle.
I have found my problem . when the form is submitted its return entity object for city but when I call. $view = View::create($form->getData(),200);
its cause problem with serializing the city object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.