SQLSTATE[42S22]:Column not found - php

I have create Formtype UniversitaireType but I have this error:
attribut condidat_id doesn't create in database when I try command php bin/console doctrine:schema:update --force (all attributs create in database except id_condidat) .. I make mapping OneToMany $condidat in entity Universitaire
code entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Condidat;
/**
* Universitaire.
*
* #ORM\Table(name="universitaires")
* #ORM\Entity(repositoryClass="AppBundle\Entity\UniversitaireEntityRepository")
*/
class Universitaire
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="date_start", type="string", length=255, nullable=true)
*/
private $dateStart;
/**
* #var string
*
* #ORM\Column(name="date_end", type="string", length=255, nullable=true)
*/
private $dateEnd;
/**
* #var string
*
* #ORM\Column(name="establishment", type="string", length=255, nullable=true, unique=true)
*/
private $establishment;
/**
* #var string
*
* #ORM\Column(name="diploma", type="string", length=255, nullable=true, unique=true)
*/
private $diploma;
/**
*#ORM\OneToMany(targetEntity="AppBundle\Entity\Condidat",mappedBy="id_universitaire",cascade={"persist"})
*#ORM\JoinColumn(name="id_condidat", referencedColumnName="id")
*/
private $condidat;
/**
* Get id.
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Add Condidat
*
* #param \AppBundle\Entity\Condidat $condidat
* #return Universitaire
*/
public function addCondidat(\AppBundle\Entity\Condidat $condidat)
{
$this->condidat[] = $condidat;
return $this;
}
/**
* Remove Condidat
*
* #param \AppBundle\Entity\Condidat $condidat
*/
public function removeCondidat(\AppBundle\Entity\Condidat $condidat)
{
$this->Condidat->removeElement($condidat);
}
/**
* Get Condidat
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCondidat()
{
return $this->condidat;
}
/**
* Set dateStart.
*
* #param string $dateStart
*
* #return Universitaire
*/
public function setdateStart($dateStart)
{
$this->dateStart = $dateStart;
return $this;
}
/**
* Get dateStart.
*
* #return string
*/
public function getdateStart()
{
return $this->dateStart;
}
/**
* Set dateEnd.
*
* #param string $dateEnd
*
* #return Universitaire
*/
public function setDateEnd($dateEnd)
{
$this->dateEnd = $dateEnd;
return $this;
}
/**
* Get dateEnd.
*
* #return string
*/
public function getdateEnd()
{
return $this->dateEnd;
}
/**
* Set establishment.
*
* #param string $establishment
*
* #return Universitaire
*/
public function setEstablishment($establishment)
{
$this->establishment = $establishment;
return $this;
}
/**
* Get establishment.
*
* #return string
*/
public function getEstablishment()
{
return $this->establishment;
}
/**
* Set diploma.
*
* #param string $diploma
*
* #return Universitaire
*/
public function setDiploma($diploma)
{
$this->diploma = $diploma;
return $this;
}
/**
* Get diploma.
*
* #return string
*/
public function getDiploma()
{
return $this->diploma;
}
public function __construct()
{
$this->ResponsableCategory = new \Doctrine\Common\Collections\ArrayCollection();
}
}
code FormType:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use AppBundle\Entity\Universitaire;
use AppBundle\Entity\Condidat;
class UniversitaireType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('dateStart')->add('dateEnd')->add('establishment')->add('diploma')
->add('condidat',null, array(
'class' => 'AppBundle:Condidat',
'choice_label' => 'condidat',));
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Universitaire',
"csrf_protection" => "false"
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_universitaire';
}
}

You have two mistakes in your "condidat" property.
You used One-To-Many (Bidirectional). If you want to have only one
candidate per "Universitaire" - then you have to use One-To-One (Bidirectional).
class Universitaire
{
## ...
/**
* Bidirectional One-To-One (Owning Side)
*
* #ORM\OneToOne(
* targetEntity="AppBundle\Entity\Condidat",
* inversedBy="universitaire",
* cascade={"persist", "remove"}
* )
* #ORM\JoinColumn(
* name="condiat_id",
* referencedColumnName="id"
* )
*/
private $condidat;
class Condidat
{
## ...
/**
* Bidirectional One-To-One (Inversed Side)
*
* #ORM\OneToOne(
* targetEntity="AppBundle\Entity\Universitaire",
* mappedBy="condidat",
* )
*/
private $universitaire;
If not, and One-To-Many was exactly you wanted to have, than you have to set in your __construct() that "condidat" is an array collection
public function __construct()
{
$this->condidat = new \Doctrine\Common\Collections\ArrayCollection();
}
If I were you I'd rather use "candidates" with "s" - to show, that this an arrayCollection.
As I've already said - your example is Bidirectional One-To-Many. In this case we use mappedBy and inversedBy. Entity, in which you set Join column is Owning side. And of course JoinColumn has to be in the "Candidate" entity. So:
class Universitaire
{
## ...
/**
* Bidirectional One-To-Many (Owning Side)
* Predicate: One Universitaire can have a lot of candidates
*
* #ORM\OneToMany(
* targetEntity="AppBundle\Entity\Condidat",
* mappedBy="universitaire",
* cascade={"persist", "remove"}
* )
*/
private $condidats;
class Condidat
{
## ...
/**
* Bidirectional Many-To-One (Owning Side)
* Predicate: Each Condidat belongs to one Universitaire
*
* #ORM\ManyToOne(
* targetEntity="AppBundle\Entity\Universitaire",
* inversedBy="condidats",
* )
* #ORM\JoinColumn(
* name="universitaire_id",
* referencedColumnName="id",
* onDelete="SET NULL"
* )
*/
private $universitaire;
I added onDelete="SET NULL". If you delete universitaire then in field universitaire of each "condidate" you will see null value.

Related

WebfactoryPolyglotBundle use causes error "No mapping found for field 'id' on class 'AppBundle\Entity\Film'."

I use the Symfony version 3.4.0 and I try to use this bundle to translate entities : https://github.com/webfactory/WebfactoryPolyglotBundle
So I created two entities ( Film and FilmTranslation ) for the test
Here my Film.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Annotation as Polyglot;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Film
*
* #ORM\Table(name="film")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilmRepository")
* #Polyglot\Locale(primary="fr_FR")
*/
class Film
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #Polyglot\TranslationCollection
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="Titre", type="string", length=255, unique=true)
* #Polyglot\Translatable
* #var string|TranslatableInterface
*/
private $titre;
/**
* #ORM\OneToMany(targetEntity="FilmTranslation", mappedBy="entity")
* #Polyglot\TranslationCollection
* #var Collection
*/
private $translations;
public function __construct()
{
$this->translations = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* #param string $titre
*
* #return Film
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Add translation
*
* #param \AppBundle\Entity\FilmTranslation $translation
*
* #return Film
*/
public function addTranslation(\AppBundle\Entity\FilmTranslation $translation)
{
$this->translations[] = $translation;
return $this;
}
/**
* Remove translation
*
* #param \AppBundle\Entity\FilmTranslation $translation
*/
public function removeTranslation(\AppBundle\Entity\FilmTranslation $translation)
{
$this->translations->removeElement($translation);
}
/**
* Get translations
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTranslations()
{
return $this->translations;
}
}
And here my FilmTranslation.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Entity\BaseTranslation;
/**
* FilmTranslation
*
* #ORM\Table(name="film_translation")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilmTranslationRepository")
* #ORM\Table(
* uniqueConstraints = {
* #ORM\UniqueConstraint(columns={"entity_id", "locale"})
* }
* )
*/
class FilmTranslation extends BaseTranslation
{
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=255, unique=true)
*/
private $titre;
/**
* #ORM\ManyToOne(targetEntity="Film", inversedBy="translations")
* #var Film
*/
protected $entity;
/**
* Set titre
*
* #param string $titre
*
* #return FilmTranslation
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set locale
*
* #param string $locale
*
* #return FilmTranslation
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* Set entity
*
* #param \AppBundle\Entity\Film $entity
*
* #return FilmTranslation
*/
public function setEntity(\AppBundle\Entity\Film $entity = null)
{
$this->entity = $entity;
return $this;
}
/**
* Get entity
*
* #return \AppBundle\Entity\Film
*/
public function getEntity()
{
return $this->entity;
}
}
I'm able to create a form but when I try to persist and flush I've the following error :
No mapping found for field 'id' on class 'AppBundle\Entity\Film'.
Is something I'm doing wrong ?
So remove #Polyglot\TranslationCollection from $id annotation like this ;)
class Film
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

Symfony Could not determine access type when using EntityType form builder

I have 2 entities Cars and Parts and I want to be able to create new Car with multiple parts. For this reason I made this form in CarsType
$builder->
add('make')->
add('model')->
add('travelledDistance')->
add('parts',EntityType::class,array(
'class' => Parts::class,
'choice_label'=>"name",
'query_builder' => function(PartsRepository $partsRepository){
return $partsRepository->getAllPartsForCarsForm();
},
'multiple' => true
));
It gives me this error
Could not determine access type for property "parts" in class
"AppBundle\Entity\Cars".
Here is my entity Cars
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Cars
*
* #ORM\Table(name="cars")
* #ORM\Entity(repositoryClass="AppBundle\Repository\CarsRepository")
*/
class Cars
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="Make", type="string", length=255)
*/
private $make;
/**
* #var string
*
* #ORM\Column(name="Model", type="string", length=255)
*/
private $model;
/**
* #var int
*
* #ORM\Column(name="TravelledDistance", type="bigint")
*/
private $travelledDistance;
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Parts", inversedBy="cars")
* #ORM\JoinTable(
* name="Parts_Cars",
* joinColumns={
* #ORM\JoinColumn(name="Part_Id", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="Car_Id", referencedColumnName="id")
* })
*/
private $parts;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set make
*
* #param string $make
*
* #return Cars
*/
public function setMake($make)
{
$this->make = $make;
return $this;
}
/**
* Get make
*
* #return string
*/
public function getMake()
{
return $this->make;
}
/**
* Set model
*
* #param string $model
*
* #return Cars
*/
public function setModel($model)
{
$this->model = $model;
return $this;
}
/**
* Get model
*
* #return string
*/
public function getModel()
{
return $this->model;
}
/**
* Set travelledDistance
*
* #param integer $travelledDistance
*
* #return Cars
*/
public function setTravelledDistance($travelledDistance)
{
$this->travelledDistance = $travelledDistance;
return $this;
}
/**
* Get travelledDistance
*
* #return int
*/
public function getTravelledDistance()
{
return $this->travelledDistance;
}
/**
* #return mixed
*/
public function getParts()
{
return $this->parts;
}
}
And in case it matters here is my Parts entity
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Parts
*
* #ORM\Table(name="parts")
* #ORM\Entity(repositoryClass="AppBundle\Repository\PartsRepository")
*/
class Parts
{
/**
* #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)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="Price", type="decimal", precision=10, scale=2)
*/
private $price;
/**
* #var int
*
* #ORM\Column(name="Quantity", type="integer")
*/
private $quantity;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Suppliers", inversedBy="parts")
* #ORM\JoinColumn(name="Supplier_Id", referencedColumnName="id")
*/
private $supplier;
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Cars", mappedBy="parts")
*/
private $cars;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Parts
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set price
*
* #param string $price
*
* #return Parts
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* #return string
*/
public function getPrice()
{
return $this->price;
}
/**
* Set quantity
*
* #param integer $quantity
*
* #return Parts
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* #return int
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* #return mixed
*/
public function getSupplier()
{
return $this->supplier;
}
/**
* #return mixed
*/
public function getCars()
{
return $this->cars;
}
public function __toString()
{
return $this->getName();
}
}
To save time here is the mapping between the 2 entities as well as the property parts in Cars
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Parts", inversedBy="cars")
* #ORM\JoinTable(
* name="Parts_Cars",
* joinColumns={
* #ORM\JoinColumn(name="Part_Id", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="Car_Id", referencedColumnName="id")
* })
*/
private $parts;
and In Parts
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Cars", mappedBy="parts")
*/
private $cars;
Here is the newAction in CarsController
/**
* Creates a new car entity.
*
* #Route("/new", name="cars_new")
* #Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$carsRepository = $this->getDoctrine()->getManager()->getRepository('AppBundle:Cars');
$car = new Cars();
$form = $this->createForm('AppBundle\Form\CarsType', $car);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($car);
$em->flush();
return $this->redirectToRoute('cars_show', array('id' => $car->getId()));
}
return $this->render('cars/new.html.twig', array(
'car' => $car,
'form' => $form->createView(),
));
}
My question is - How can I make it so that I can create new Car with several Parts and to save the relationship in the database so that when I retrieve the Car I can get the parts as well?
Basically how to make it so when I create a new car the relationship is saved in the parts_cars table which holds the id's?
Let Doctrine do the JOIN work
Since you're doing a ManyToMany (EntityToEntity), the #JoinColumn directives are not needed.
Try removing it from the Cars entity.
Cars
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Parts", inversedBy="cars")
*/
$parts
The only cases in which you need to specify the JoinColumns are:
Joining against self
Joining where the primary key is not the Entity ID.
Need to define fields in the join_table
Would be MUCH easier in this case to do A OneToMany J ManyToOne B
Since you're doing none of the above, Doctrine is having trouble accessing the Entities' IDENTITY fields for the join.

Symfony 3 multiple entities in one form

I stuck with this for past two days. I am creating bus traveling website. Currently I am in phase of adding new bus to database.
One bus (or all of them) have many amenities (like AirCon, WiFi, Kitchen, ...) and each of these needs to have custom prices. Let says that our bus have AirCon. AirCon for this bus will cost 50 USD but for other bus AirCon might cost 100 USD. To do that I created 3 Entities (Bus Vehicles Entity, Amenities Entity and Bus Vehicles Amenities Entity). Inside Amenities Entity I iwll store Id of bus, ID of amenity and price for that amenity for that bus ID.
Problem is that I can't get it work. I got blank HTML element when rendering form for that field.I can't add or delete multiple amenities and I am not sure why it is not working.
Does someone knows where is the problem?
Here is the code that I use:
Amenities Entity:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Amenities
*
* #ORM\Table(name="amenities", indexes={#ORM\Index(name="administrator_id", columns={"administrator_id"})})
* #ORM\Entity
*/
class Amenities
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100, nullable=true)
*/
private $name;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt = 'CURRENT_TIMESTAMP';
/**
* #var \DateTime
*
* #ORM\Column(name="modified_at", type="datetime", nullable=false)
*/
private $modifiedAt = 'CURRENT_TIMESTAMP';
/**
* #var \AppBundle\Entity\Users
*
* #ORM\ManyToOne(targetEntity="Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="administrator_id", referencedColumnName="id")
* })
*/
private $administrator;
/**
* #ORM\OneToMany(targetEntity="BusVehiclesAmenities", mappedBy="amenities")
*/
private $amenities;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Amenities
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
*
* #return Amenities
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Constructor
*/
public function __construct()
{
$this->amenities = new ArrayCollection();
}
/**
* Add amenities
*
* #param \AppBundle\Entity\BusVehiclesAmenities $amenities
* #return Amenities
*/
public function addAmenities(BusVehiclesAmenities $amenities)
{
$this->amenities[] = $amenities;
return $this;
}
/**
* Remove amenities
*
* #param \AppBundle\Entity\BusVehiclesAmenities $amenities
*/
public function removeAmenities(BusVehiclesAmenities $amenities)
{
$this->amenities->removeElement($amenities);
}
/**
* Get amenities_bus_vehicles
*
* #return ArrayCollection
*/
public function getAmenities()
{
return $this->amenities;
}
}
Bus Vehicles Entity:
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* BusVehicles
*
* #ORM\Table(name="bus_vehicles", indexes={#ORM\Index(name="company_id", columns={"company_id"}), #ORM\Index(name="bus_type", columns={"bus_type"}), #ORM\Index(name="fuel_type", columns={"fuel_type"}), #ORM\Index(name="emission_class", columns={"emission_class"})})
* #ORM\Entity
*/
class BusVehicles
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="licence_plate", type="string", length=100, nullable=false)
*/
private $licencePlate;
/**
* #var \AppBundle\Entity\Companies
*
* #ORM\ManyToOne(targetEntity="Companies")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="company_id", referencedColumnName="id")
* })
*/
private $company;
/**
* #var \AppBundle\Entity\BusTypes
*
* #ORM\ManyToOne(targetEntity="BusTypes", inversedBy="busType")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="bus_type", referencedColumnName="id")
* })
*/
/**
* #ORM\OneToMany(targetEntity="BusVehiclesAmenities", mappedBy="bus")
*/
private $busVehiclesAmenities;
public function __construct()
{
$this->busVehiclesAmenities = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add busVehiclesAmenities
*
* #param \AppBundle\Entity\BusVehiclesAmenities busVehiclesAmenities
* #return BusVehicles
*/
public function addBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
$this->busVehiclesAmenities[] = $busVehiclesAmenities;
return $this;
}
/**
* Remove busVehiclesAmenities
*
* #param \AppBundle\Entity\BusVehiclesAmenities $busVehiclesAmenities
*/
public function removeBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
$this->busVehiclesAmenities->removeElement($busVehiclesAmenities);
}
/**
* Get busVehiclesAmenities
*
* #return ArrayCollection
*/
public function getBusVehiclesAmenities()
{
return $this->busVehiclesAmenities;
}
/**
* Set licencePlate
*
* #param string $licencePlate
*
* #return BusVehicles
*/
public function setLicencePlate($licencePlate)
{
$this->licencePlate = $licencePlate;
return $this;
}
/**
* Set company
*
* #param \AppBundle\Entity\Companies $company
*
* #return BusVehicles
*/
public function setCompany(Companies $company = null)
{
$this->company = $company;
return $this;
}
/**
* Get company
*
* #return \AppBundle\Entity\Companies
*/
public function getCompany()
{
return $this->company;
}
}
Bus Amenities Entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* BusVehiclesAmenities
*
* #ORM\Table(name="bus_vehicles_amenities", indexes={#ORM\Index(name="amenities_id", columns={"amenities_id"}), #ORM\Index(name="bus_id", columns={"bus_id"})})
* #ORM\Entity
*/
class BusVehiclesAmenities
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
*
* #ORM\ManyToOne(targetEntity="BusVehicles", inversedBy="busVehiclesAmenities")
* #ORM\JoinColumn(name="bus_id", referencedColumnName="id")
*
*/
private $bus;
/**
*
* #ORM\ManyToOne(targetEntity="Amenities", inversedBy="amenities")
* #ORM\JoinColumn(name="amenities_id", referencedColumnName="id")
*
*/
private $amenities;
/**
* #var float
* #ORM\Column(name="price", type="float", scale=2)
*/
protected $price;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set Bus
*
* #param \AppBundle\Entity\BusVehicles
*
* #return BusVehiclesAmenities
*/
public function setBus($bus)
{
$this->bus = $bus;
return $this;
}
/**
* Get busId
*
* #return integer
*/
public function getBus()
{
return $this->bus;
}
/**
* Set amenities
*
* #param \AppBundle\Entity\Amenities
*
* #return BusVehiclesAmenities
*/
public function setAmenities($amenities)
{
$this->amenities = $amenities;
return $this;
}
/**
* Get amenities
*
* #return \AppBundle\Entity\Amenities
*/
public function getAmenities()
{
return $this->amenities;
}
/**
* Set price
*
* #param float $price
*
* #return BusVehiclesAmenities
*/
public function sePrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* #return float
*/
public function getPrice()
{
return $this->price;
}
}
FORMS:
Add new bus form:
<?php
namespace AdminBundle\Form\Type;
use AdminBundle\Form\Type\BusVehiclesAmenitiesType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class BusVehiclesType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('licencePlate')
->add('company', EntityType::class, array(
'class' => 'AppBundle:Companies',
'choice_label' => 'name',
->add('busVehiclesAmenities', CollectionType::class, array(
'entry_type' => BusVehiclesAmenitiesType::class,
'allow_add' => true,
));
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\BusVehicles'
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'adminbundle_busvehicles';
}
}
Bus Vehicles Amenities Form
<?php
namespace AdminBundle\Form\Type;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Entity\BusVehiclesAmenities;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BusVehiclesAmenitiesType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('price', MoneyType::class, array(
'scale' => 2,
))
->add('amenities', EntityType::class, array(
'class' =>'AppBundle:Amenities',
'choice_label' => 'name',
))
;
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => BusVehiclesAmenities::class
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_busvehiclesamenities';
}
}
I would not use the entity classes for form binding.
I would create a new class, (e.g. BusVehicle) which contains all properties that need to be on the form (particular fields) and use that as the 'data_class'. This way you would not only solve your problem, but you would also decouple presentation layer from business layer (see Multitier Architecture).
I usually put these classes in Form/Model directory.
In your case the class would be Form/Model/BusVehicle, and it would have $amenities property.
The amenities would be an array of Form/Model/Amenity objects.
You would probably need to use embedded forms just don't use entity classes as 'data_object'. After a successful bind, you instantiate and populate entities for persist or update.
And you don't need the third entity ("Bus Vehicles Amenities").

Doctrine 2 Issue

I am trying to learn the framework Doctrine 2. I have a Model in MySQL and implement it in Doctrine. After the implementation of the dependency between the two classes Task and Dropdown list, it doesnt operate.
My code is:
<?php
use Doctrine\Common\Collections;
use Doctrine\ORM\Mapping AS ORM;
/**
* Task
*
* #Table(name="task")
* #Entity
*/
class Task
{
/**
* #var integer
*
* #Column(name="ID", type="integer", nullable=false)
* #Id
* #GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var Dropdownlist
* #ORM\OneToMany(targetEntity="Dropdownlist", mappedBy="tasks")
* #ORM\JoinColumn(name="priority", referencedColumnName="id")
*/
protected $priority;
/**
* #var string
*
* #Column(name="Label", type="string", length=45, nullable=true)
*/
private $label;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set priority
*
* #param integer $priority
*
* #return Task
*/
public function setPriority($priority)
{
$this->priority = $priority;
return $this;
}
/**
* Get priority
*
* #return integer
*/
public function getPriority()
{
return $this->priority;
}
/**
* Set label
*
* #param string $label
*
* #return Task
*/
public function setLabel($label)
{
$this->label = $label;
return $this;
}
/**
* Get label
*
* #return string
*/
public function getLabel()
{
return $this->label;
}
}
/**
* Dropdownlist
*
* #Table(name="dropdownlist")
* #Entity
*/
class Dropdownlist
{
/**
* #var integer
*
* #Column(name="ID", type="integer")
* #Id
* #GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #Column(name="PriorityLabel", type="string", length=45, nullable=true)
*/
private $prioritylabel;
/*
* #var ArrayCollection
* #ORM\ManyToOne(targetEntity="Task", inversedBy="priority")
*/
protected $tasks;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set prioritylabel
*
* #param string $prioritylabel
*
* #return Dropdownlist
*/
public function setPrioritylabel($prioritylabel)
{
$this->prioritylabel = $prioritylabel;
return $this;
}
/**
* Get prioritylabel
*
* #return string
*/
public function getPrioritylabel()
{
return $this->prioritylabel;
}
public function getTasts(){
return $this->tasks;
}
public function __construct()
{
}
}
Problem:
The database schema is not in sync with the current mapping file.
What is the reason?
Where is the problem?
Best regards
It means your database is not in sync with your current mapping.
Run doctrine:schema:update --complete --dump-sql to see which changes need to be done. You can also run doctrine:schema:update --complete --force to deploy the changes directly.

Error in Symfony 2 when create object of child entity class

I have a file /src/AppBundle/Entity/Questionnaire.php with 3 Entity classes, where I'm trying to implement Single table inheritance with Doctrine 2 on Symfony 2.7. Questionnaire is a parent abstract class, and there are 2 child classes FirstQuestions and SecondsQuestions that extends Questionnaire. I choosed this model because I need to write data in table in 2 steps. The code of this file is below:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Questionnaire
*
* #ORM\Entity
* #ORM\Table(name="questionnaire")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({"firstquestions" = "FirstQuestions", "secondquestions" = "SecondQuestions"})
*/
abstract class Questionnaire {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
/**
* FirstQuestions
*/
class FirstQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="firstName", type="string", length=64)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="lastName", type="string", length=64)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=32)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="birthday", type="date")
*/
private $birthday;
/**
* #var integer
*
* #ORM\Column(name="shoeSize", type="integer")
*/
private $shoeSize;
/**
* Set firstName
*
* #param string $firstName
*
* #return Questionnaire
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
*
* #return Questionnaire
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* #param string $email
*
* #return Questionnaire
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set birthday
*
* #param \DateTime $birthday
*
* #return Questionnaire
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* #return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Set shoeSize
*
* #param integer $shoeSize
*
* #return Questionnaire
*/
public function setShoeSize($shoeSize)
{
$this->shoeSize = $shoeSize;
return $this;
}
/**
* Get shoeSize
*
* #return integer
*/
public function getShoeSize()
{
return $this->shoeSize;
}
}
/**
* SecondQuestions
*/
class SecondQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="favoriteIceCream", type="string", length=128)
*/
private $favoriteIceCream;
/**
* #var string
*
* #ORM\Column(name="favoriteSuperHero", type="string", length=32)
*/
private $favoriteSuperHero;
/**
* #var string
*
* #ORM\Column(name="favoriteMovieStar", type="string", length=64)
*/
private $favoriteMovieStar;
/**
* #var \DateTime
*
* #ORM\Column(name="endOfTheWorld", type="date")
*/
private $endOfTheWorld;
/**
* #var string
*
* #ORM\Column(name="superBowlWinner", type="string", length=32)
*/
private $superBowlWinner;
/**
* Set favoriteIceCream
*
* #param string $favoriteIceCream
*
* #return Questionnaire
*/
public function setFavoriteIceCream($favoriteIceCream)
{
$this->favoriteIceCream = $favoriteIceCream;
return $this;
}
/**
* Get favoriteIceCream
*
* #return string
*/
public function getFavoriteIceCream()
{
return $this->favoriteIceCream;
}
/**
* Set favoriteSuperHero
*
* #param string $favoriteSuperHero
*
* #return Questionnaire
*/
public function setFavoriteSuperHero($favoriteSuperHero)
{
$this->favoriteSuperHero = $favoriteSuperHero;
return $this;
}
/**
* Get favoriteSuperHero
*
* #return string
*/
public function getFavoriteSuperHero()
{
return $this->favoriteSuperHero;
}
/**
* Set favoriteMovieStar
*
* #param string $favoriteMovieStar
*
* #return Questionnaire
*/
public function setFavoriteMovieStar($favoriteMovieStar)
{
$this->favoriteMovieStar = $favoriteMovieStar;
return $this;
}
/**
* Get favoriteMovieStar
*
* #return string
*/
public function getFavoriteMovieStar()
{
return $this->favoriteMovieStar;
}
/**
* Set endOfTheWorld
*
* #param \DateTime $endOfTheWorld
*
* #return Questionnaire
*/
public function setEndOfTheWorld($endOfTheWorld)
{
$this->endOfTheWorld = $endOfTheWorld;
return $this;
}
/**
* Get endOfTheWorld
*
* #return \DateTime
*/
public function getEndOfTheWorld()
{
return $this->endOfTheWorld;
}
/**
* Set superBowlWinner
*
* #param string $superBowlWinner
*
* #return Questionnaire
*/
public function setSuperBowlWinner($superBowlWinner)
{
$this->superBowlWinner = $superBowlWinner;
return $this;
}
/**
* Get superBowlWinner
*
* #return string
*/
public function getSuperBowlWinner()
{
return $this->superBowlWinner;
}
}
So the problem is when I'm trying to create object of child class(FirstQuestions or SecondsQuestions) in method of controller, Symfony displays me error "500 Internal Server Error". The code of controller with method is below:
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Questionnaire;
use AppBundle\Entity\FirstQuestions;
use AppBundle\Entity\SecondQuestions;
class TestController extends Controller
{
/**
* #Route("/test", name="test")
*/
public function indexAction(Request $request)
{
$item = new FirstQuestions(); // everything works well without this line
return new Response(
'ok'
);
}
}
Maybe I am doing something wrong or didn't set any important annotation. Can anyone help me?
This will be one of those annoying little oversight errors - an extra semi-colon or something somewhere that you're not looking for it. I'm creating this additional answer so that I can give you exactly the code I'm using. Hopefully, you'll be able to cut-and-paste, replace your own files with this new code, and it will magically start working.
First - to prove the point, here's my (modified) output:
Veromo\Bundle\CoreBundle\Entity\FirstQuestions Object
(
[firstName:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[lastName:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[email:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[birthday:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[shoeSize:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[id:Veromo\Bundle\CoreBundle\Entity\Questionnaire:private] =>
)
Which shows that all I'm doing differently to you is using my own dev environment's namespaces.
AppBundle\Entity\Questionnaire.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Questionnaire
*
* #ORM\Entity
* #ORM\Table(name="questionnaire")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({"questionnaire"="Questionnaire", "firstquestions" = "FirstQuestions", "secondquestions" = "SecondQuestions"})
*/
abstract class Questionnaire {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
AppBundle\Entity\FirstQuestions.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* FirstQuestions
* #ORM\Entity()
*/
class FirstQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="firstName", type="string", length=64)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="lastName", type="string", length=64)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=32)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="birthday", type="date")
*/
private $birthday;
/**
* #var integer
*
* #ORM\Column(name="shoeSize", type="integer")
*/
private $shoeSize;
/**
* Set firstName
*
* #param string $firstName
*
* #return Questionnaire
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
*
* #return Questionnaire
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* #param string $email
*
* #return Questionnaire
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set birthday
*
* #param \DateTime $birthday
*
* #return Questionnaire
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* #return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Set shoeSize
*
* #param integer $shoeSize
*
* #return Questionnaire
*/
public function setShoeSize($shoeSize)
{
$this->shoeSize = $shoeSize;
return $this;
}
/**
* Get shoeSize
*
* #return integer
*/
public function getShoeSize()
{
return $this->shoeSize;
}
}
AppBundle\Entity\SecondQuestions.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SecondQuestions
* #ORM\Entity()
*/
class SecondQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="favoriteIceCream", type="string", length=128)
*/
private $favoriteIceCream;
/**
* #var string
*
* #ORM\Column(name="favoriteSuperHero", type="string", length=32)
*/
private $favoriteSuperHero;
/**
* #var string
*
* #ORM\Column(name="favoriteMovieStar", type="string", length=64)
*/
private $favoriteMovieStar;
/**
* #var \DateTime
*
* #ORM\Column(name="endOfTheWorld", type="date")
*/
private $endOfTheWorld;
/**
* #var string
*
* #ORM\Column(name="superBowlWinner", type="string", length=32)
*/
private $superBowlWinner;
/**
* Set favoriteIceCream
*
* #param string $favoriteIceCream
*
* #return Questionnaire
*/
public function setFavoriteIceCream($favoriteIceCream)
{
$this->favoriteIceCream = $favoriteIceCream;
return $this;
}
/**
* Get favoriteIceCream
*
* #return string
*/
public function getFavoriteIceCream()
{
return $this->favoriteIceCream;
}
/**
* Set favoriteSuperHero
*
* #param string $favoriteSuperHero
*
* #return Questionnaire
*/
public function setFavoriteSuperHero($favoriteSuperHero)
{
$this->favoriteSuperHero = $favoriteSuperHero;
return $this;
}
/**
* Get favoriteSuperHero
*
* #return string
*/
public function getFavoriteSuperHero()
{
return $this->favoriteSuperHero;
}
/**
* Set favoriteMovieStar
*
* #param string $favoriteMovieStar
*
* #return Questionnaire
*/
public function setFavoriteMovieStar($favoriteMovieStar)
{
$this->favoriteMovieStar = $favoriteMovieStar;
return $this;
}
/**
* Get favoriteMovieStar
*
* #return string
*/
public function getFavoriteMovieStar()
{
return $this->favoriteMovieStar;
}
/**
* Set endOfTheWorld
*
* #param \DateTime $endOfTheWorld
*
* #return Questionnaire
*/
public function setEndOfTheWorld($endOfTheWorld)
{
$this->endOfTheWorld = $endOfTheWorld;
return $this;
}
/**
* Get endOfTheWorld
*
* #return \DateTime
*/
public function getEndOfTheWorld()
{
return $this->endOfTheWorld;
}
/**
* Set superBowlWinner
*
* #param string $superBowlWinner
*
* #return Questionnaire
*/
public function setSuperBowlWinner($superBowlWinner)
{
$this->superBowlWinner = $superBowlWinner;
return $this;
}
/**
* Get superBowlWinner
*
* #return string
*/
public function getSuperBowlWinner()
{
return $this->superBowlWinner;
}
}
AppBundle\Controller\TestController.php
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Questionnaire;
use AppBundle\Entity\FirstQuestions;
use AppBundle\Entity\SecondQuestions;
class TestController extends Controller
{
/**
* #Route("/test",name="test")
*/
public function indexAction( Request $request )
{
$item = new FirstQuestions();
return new Response(
'<pre>'.print_r( $item, true ).'</pre>'
);
}
}
And just to be sure ...
app\config\routing.yml
test:
resource: "#AppBundle/Controller/TestController.php"
type: annotation
It's GOT to be some stupid, annoying little error that nobody is looking for.
Hope this helps ...
ALL Entity classes which are part of the mapped entity hierarchy need to be specified in the #DiscriminatorMap. So, yes, your annotation is incorrect.
Doctrine Single Table Inheritance
EDIT
You have another annotations error - neither of your subclasses has an #Entity annotation:
/**
* FirstQuestions
* #ORM\Entity()
*/
class FirstQuestions extends Questionnaire {
/**
* SecondQuestions
* #ORM\Entity()
*/
class SecondQuestions extends Questionnaire {
After fixing this I was able to use Doctrine's Schema Update tool to build the tables AND successfully created a FirstQuestions object.

Categories