public function getForm($car = null)
{
return $this->createForm(new CarType(), $car);
}
/**
* Presents the form to use to create a new car.
*
* @ApiDoc(
* resource = true,
* statusCodes = {
* 200 = "Returned when successful"
* }
* )
*
* @return FormTypeInterface
*/
public function newCarAction()
{
return $this->getForm();
}
/**
* Creates a new car from the submitted data.
*
* @ApiDoc(
* resource = true,
* input = {
* "class" = "AppBundle\Form\API\CarType",
* "name" = ""
* },
* output = "AppBundle\Entity\Car",
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned when the form has errors"
* }
* )
*
* @param Request $request the request object
*
* @return FormTypeInterface|RouteRedirectView
*/
public function postCarAction(Request $request)
{
$car = new Car();
$form = $this->getForm($car);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($car);
$em->flush();
$view = $this->routeRedirectView('api_get_car', array('id' => $car->getId()));
} else {
$view = $this->view($form);
}
return $view;
}
/**
* Car.
*
* @ORM\Table("car")
* @ORM\Entity
*/
class Car
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="quantity_fuel", type="integer")
*/
private $quantityFuel;
/**
* @var string
*
* @ORM\Column(name="model", type="string", length=255)
*/
private $model;
/**
* @var string
*
* @ORM\Column(name="brand", type="string", length=255)
*/
private $brand;
/**
* @var string
*
* @ORM\Column(name="license_plate", type="string", length=25)
*/
private $licensePlate;
/**
* @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="cars")
* @ORM\JoinColumn(name="owner_id", referencedColumnName="id")
**/
private $owner;
/**
* @ORM\OneToMany(targetEntity="Path", mappedBy="car")
**/
private $paths;
/**
* @ORM\OneToMany(targetEntity="Cost", mappedBy="car")
**/
private $costs;
/**
* Constructor.
*/
public function __construct()
{
$this->paths = new \Doctrine\Common\Collections\ArrayCollection();
$this->costs = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set quantityFuel.
*
* @param int $quantityFuel
*
* @return Car
*/
public function setQuantityFuel($quantityFuel)
{
$this->quantityFuel = $quantityFuel;
return $this;
}
/**
* Get quantityFuel.
*
* @return int
*/
public function getQuantityFuel()
{
return $this->quantityFuel;
}
/**
* Set model.
*
* @param string $model
*
* @return Car
*/
public function setModel($model)
{
$this->model = $model;
return $this;
}
/**
* Get model.
*
* @return string
*/
public function getModel()
{
return $this->model;
}
/**
* Set brand.
*
* @param string $brand
*
* @return Car
*/
public function setBrand($brand)
{
$this->brand = $brand;
return $this;
}
/**
* Get brand.
*
* @return string
*/
public function getBrand()
{
return $this->brand;
}
/**
* Set licensePlate.
*
* @param string $licensePlate
*
* @return Car
*/
public function setLicensePlate($licensePlate)
{
$this->licensePlate = $licensePlate;
return $this;
}
/**
* Get licensePlate.
*
* @return string
*/
public function getLicensePlate()
{
return $this->licensePlate;
}
/**
* Add paths.
*
* @param \AppBundle\Entity\Path $paths
*
* @return Car
*/
public function addPath(\AppBundle\Entity\Path $paths)
{
$this->paths[] = $paths;
return $this;
}
/**
* Remove paths.
*
* @param \AppBundle\Entity\Path $paths
*/
public function removePath(\AppBundle\Entity\Path $paths)
{
$this->paths->removeElement($paths);
}
/**
* Get paths.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPaths()
{
return $this->paths;
}
/**
* Add costs.
*
* @param \AppBundle\Entity\Cost $costs
*
* @return Car
*/
public function addCost(\AppBundle\Entity\Cost $costs)
{
$this->costs[] = $costs;
return $this;
}
/**
* Remove costs.
*
* @param \AppBundle\Entity\Cost $costs
*/
public function removeCost(\AppBundle\Entity\Cost $costs)
{
$this->costs->removeElement($costs);
}
/**
* Get costs.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCosts()
{
return $this->costs;
}
/**
* Set owner.
*
* @param \Application\Sonata\UserBundle\Entity\User $owner
*
* @return Car
*/
public function setOwner(\Application\Sonata\UserBundle\Entity\User $owner = null)
{
$this->owner = $owner;
return $this;
}
/**
* Get owner.
*
* @return \Application\Sonata\UserBundle\Entity\User
*/
public function getOwner()
{
return $this->owner;
}
}
class CarType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('quantityFuel')
->add('model')
->add('brand')
->add('licensePlate')
->add('owner')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Car',
'csrf_protection' => false,
));
}
/**
* @return string
*/
public function getName()
{
return 'app_api_car';
}
}