FOSUserBundle - Embed formtype - forms

help me please ! I would like to embed form of the FOSUserBundle form in another form builder of an entity. When I do it there is no validation test in user entity. i m novice in symfony.
exemple :
->add('user', RegistrationType::class)
class FolderType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user', RegistrationType::class)
->add('firstname', TextType::class)
->add('lastname', TextType::class)
->add('dateBirth', BirthdayType::class)
->add('nationality', CountryType::class,
array(
'preferred_choices' => array(
'France', 'FR')
))
->add('phone', NumberType::class)
->add('save', SubmitType::class);
}
add function in controller :
public function addAction(Request $request, School $schoolId)
{
$em = $this->getDoctrine()->getManager();
$school = $em->getRepository('SchoolBundle:School')->findOneById($schoolId);
$candidat = new candidat();
//School Parameter
$candidat->setSchool($schoolId);
$form = $this->createForm('CandidacyBundle\Form\Onepage\FolderType', $candidat, array('schoolId' => $schoolId));
if($request->isMethod('POST') && $form->handleRequest($request)->isValid())
{
//exit(\Doctrine\Common\Util\Debug::dump($candidat));
$em->persist($candidat);
$em->flush();
$this->get('session')->getFlashBag()->add('notice','Yours new candidacy is added !');
return $this->redirectToRoute('candidacy_home');
}
return $this->render('CandidacyBundle:Folder_B:folder.html.twig', array(
'school' => $school,
'new_folder' => $form->createView()
));
}
REGISTRATION TYPE
namespace UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class RegistrationType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder;
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
}
CANDIDAT ENTITY
namespace CandidacyBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Candidat
*
* #ORM\Table()
* #ORM\Entity
*/
class Candidat
{
//x - x - x - x - x - x - x - x - x - x - x - x - x - x - OBJ
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity="UserBundle\Entity\User", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
* #Assert\Valid
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity="SchoolBundle\Entity\School")
* #ORM\JoinColumn(name="id_school", referencedColumnName="id", nullable=true)
*/
private $school;
/**
* #var boolean
*
* #ORM\Column(name="state", type="boolean")
*/
private $state = true;
/**
* #var boolean
*
* #ORM\Column(name="state_blacklist", type="boolean")
*/
private $stateBlacklist = false;
/**
* #var \DateTime
*
* #ORM\Column(name="date_entry", type="datetime")
*/
private $dateEntry;
/**
* #var string
* #Assert\NotBlank()
* #Assert\Length(min=3)
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $firstname;
/**
* #var string
* #Assert\NotBlank(message="Vous devez indiquer votre nom")
* #Assert\Length(min=3)
* #Assert\Regex(
* pattern="/\d/",
* match=false,
* message="Uniquement des lettres"
* )
* #ORM\Column(name="lastname", type="string", length=255, nullable=true)
*/
private $lastname;
/**
* #var \date
* #ORM\Column(name="date_birth", type="date", nullable=true)
*/
private $dateBirth;
/**
* #var string
*
* #ORM\Column(name="nationality", type="string", length=255, nullable=true)
*/
private $nationality;
/**
* #var integer
*
* #ORM\Column(name="phone", type="integer", nullable=true)
*/
private $phone;
/**
* #var string
*
* #ORM\Column(name="adress_street", type="string", length=255, nullable=true)
*/
private $adress_street;
/**
* #var string
*
* #ORM\Column(name="adress_city", type="string", length=255, nullable=true)
*/
private $adress_city;
/**
* #var string
*
* #ORM\Column(name="adress_region", type="string", length=255, nullable=true)
*/
private $adress_region;
/**
* #var string
*
* #ORM\Column(name="adress_country", type="string", length=255, nullable=true)
*/
private $adress_country;
/**
* #var integer
*
* #ORM\Column(name="adress_zip", type="smallint", nullable=true)
*/
private $adress_zip;
/**
* #ORM\ManyToOne(targetEntity="SchoolBundle\Entity\Level", cascade={"persist"})
* #ORM\JoinColumn(name="study_current_level", referencedColumnName="id", nullable=true)
*/
private $studyCurrentLevel;
/**
* #var string
*
* #ORM\Column(name="study_current_graduate_name", type="string", length=50)
*/
private $studyCurrentGraduateName;
/**
* #ORM\ManyToOne(targetEntity="SchoolBundle\Entity\Graduate", cascade={"persist"})
* #ORM\JoinColumn(name="study_current_graduate", referencedColumnName="id", nullable=true )
*/
private $studyCurrentGraduate;
/**
* #ORM\ManyToOne(targetEntity="SchoolBundle\Entity\Graduate")
* #ORM\JoinColumn(name="study_widh_graduate", referencedColumnName="id", nullable=true )
*/
private $studyWishGraduate;
/**
* #ORM\ManyToOne(targetEntity="SchoolBundle\Entity\Sector")
* #ORM\JoinColumn(nullable=true)
*/
private $studyWishSector;
/**
* #ORM\ManyToMany(targetEntity="SchoolBundle\Entity\Speciality")
* #ORM\JoinTable(name="candidat_Speciality")
*/
private $studyWishSpecialities;
/**
* #ORM\ManyToOne(targetEntity="SchoolBundle\Entity\Section")
* #ORM\JoinColumn(nullable=true)
*/
private $studyWishSection;
/**
* #ORM\ManyToMany(targetEntity="SchoolBundle\Entity\Diploma", cascade={"persist"})
* #ORM\JoinTable(name="candidat_diploma")
*/
private $studyWishDiplomas;
/**
* #ORM\ManyToOne(targetEntity="SchoolBundle\Entity\Rhythm", cascade={"persist"})
* #ORM\JoinColumn(name="study_wish_rhythm", referencedColumnName="id", nullable=true)
*/
private $studyWishRhythm;
/**
* #var boolean
*
* #ORM\Column(name="study_wish_place_study", type="boolean", nullable=true)
*/
private $studyWishPlaceStudy;
/**
* #ORM\OneToOne(targetEntity="CandidacyBundle\Entity\Attachment", cascade={"all"})
* #ORM\JoinColumn(name="file_picture", nullable=true)
*/
private $picture;
/**
* #ORM\OneToOne(targetEntity="CandidacyBundle\Entity\Attachment", cascade={"all"})
* #ORM\JoinColumn(name="file_cv", nullable=true)
*/
private $cv;
/**
* #ORM\ManyToMany(targetEntity="CandidacyBundle\Entity\Attachment",cascade={"persist"})
* #ORM\JoinTable(name="candidat_attachment")
*/
private $attachments;
//x - x - x - x - x - x - x - x - x - x - x - x - x - x - CONSTRUCT
public function __construct()
{
$this->dateEntry = new \DateTime();
$this->attachments = new ArrayCollection();
}
//x - x - x - x - x - x - x - x - x - x - x - x - x - x - GET/SET
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set state
*
* #param boolean $state
*
* #return Candidat
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get state
*
* #return boolean
*/
public function getState()
{
return $this->state;
}
/**
* Set stateBlacklist
*
* #param boolean $stateBlacklist
*
* #return Candidat
*/
public function setStateBlacklist($stateBlacklist)
{
$this->stateBlacklist = $stateBlacklist;
return $this;
}
/**
* Get stateBlacklist
*
* #return boolean
*/
public function getStateBlacklist()
{
return $this->stateBlacklist;
}
/**
* Set dateEntry
*
* #param \DateTime $dateEntry
*
* #return Candidat
*/
public function setDateEntry($dateEntry)
{
$this->dateEntry = $dateEntry;
return $this;
}
/**
* Get dateEntry
*
* #return \DateTime
*/
public function getDateEntry()
{
return $this->dateEntry;
}
/**
* Set firstname
*
* #param string $firstname
*
* #return Candidat
*/
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 Candidat
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* #return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set dateBirth
*
* #param \DateTime $dateBirth
*
* #return Candidat
*/
public function setDateBirth($dateBirth)
{
$this->dateBirth = $dateBirth;
return $this;
}
/**
* Get dateBirth
*
* #return \DateTime
*/
public function getDateBirth()
{
return $this->dateBirth;
}
/**
* Set email
*
* #param string $email
*
* #return Candidat
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set nationality
*
* #param string $nationality
*
* #return Candidat
*/
public function setNationality($nationality)
{
$this->nationality = $nationality;
return $this;
}
/**
* Get nationality
*
* #return string
*/
public function getNationality()
{
return $this->nationality;
}
/**
* Set phone
*
* #param integer $phone
*
* #return Candidat
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* #return integer
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set adressStreet
*
* #param string $adressStreet
*
* #return Candidat
*/
public function setAdressStreet($adressStreet)
{
$this->adress_street = $adressStreet;
return $this;
}
/**
* Get adressStreet
*
* #return string
*/
public function getAdressStreet()
{
return $this->adress_street;
}
/**
* Set adressCity
*
* #param string $adressCity
*
* #return Candidat
*/
public function setAdressCity($adressCity)
{
$this->adress_city = $adressCity;
return $this;
}
/**
* Get adressCity
*
* #return string
*/
public function getAdressCity()
{
return $this->adress_city;
}
/**
* Set adressRegion
*
* #param string $adressRegion
*
* #return Candidat
*/
public function setAdressRegion($adressRegion)
{
$this->adress_region = $adressRegion;
return $this;
}
/**
* Get adressRegion
*
* #return string
*/
public function getAdressRegion()
{
return $this->adress_region;
}
/**
* Set adressCountry
*
* #param string $adressCountry
*
* #return Candidat
*/
public function setAdressCountry($adressCountry)
{
$this->adress_country = $adressCountry;
return $this;
}
/**
* Get adressCountry
*
* #return string
*/
public function getAdressCountry()
{
return $this->adress_country;
}
/**
* Set adressZip
*
* #param integer $adressZip
*
* #return Candidat
*/
public function setAdressZip($adressZip)
{
$this->adress_zip = $adressZip;
return $this;
}
/**
* Get adressZip
*
* #return integer
*/
public function getAdressZip()
{
return $this->adress_zip;
}
/**
* Set studyCurrentLevel
*
* #param string $studyCurrentLevel
*
* #return Candidat
*/
public function setStudyCurrentLevel($studyCurrentLevel)
{
$this->studyCurrentLevel = $studyCurrentLevel;
return $this;
}
/**
* Get studyCurrentLevel
*
* #return string
*/
public function getStudyCurrentLevel()
{
return $this->studyCurrentLevel;
}
/**
* Set studyCurrentGraduateName
*
* #param string $studyCurrentGraduateName
*
* #return Candidat
*/
public function setStudyCurrentGraduateName($studyCurrentGraduateName)
{
$this->studyCurrentGraduateName = $studyCurrentGraduateName;
return $this;
}
/**
* Get studyCurrentGraduateName
*
* #return string
*/
public function getStudyCurrentGraduateName()
{
return $this->studyCurrentGraduateName;
}
/**
* Set studyCurrentGraduate
*
* #param string $studyCurrentGraduate
*
* #return Candidat
*/
public function setStudyCurrentGraduate($studyCurrentGraduate)
{
$this->studyCurrentGraduate = $studyCurrentGraduate;
return $this;
}
/**
* Get studyCurrentGraduate
*
* #return string
*/
public function getStudyCurrentGraduate()
{
return $this->studyCurrentGraduate;
}
/**
* Set studyWishGraduate
*
* #param string $studyWishGraduate
*
* #return Candidat
*/
public function setStudyWishGraduate($studyWishGraduate)
{
$this->studyWishGraduate = $studyWishGraduate;
return $this;
}
/**
* Get studyWishGraduate
*
* #return string
*/
public function getStudyWishGraduate()
{
return $this->studyWishGraduate;
}
/**
* Set studyWishSector
*
* #param string $studyWishSector
*
* #return Candidat
*/
public function setStudyWishSector($studyWishSector)
{
$this->studyWishSector = $studyWishSector;
return $this;
}
/**
* Get studyWishSector
*
* #return string
*/
public function getStudyWishSector()
{
return $this->studyWishSector;
}
/**
* Set studyWishRhythm
*
* #param string $studyWishRhythm
*
* #return Candidat
*/
public function setStudyWishRhythm($studyWishRhythm)
{
$this->studyWishRhythm = $studyWishRhythm;
return $this;
}
/**
* Get studyWishRhythm
*
* #return string
*/
public function getStudyWishRhythm()
{
return $this->studyWishRhythm;
}
/**
* Set studyWishPlaceStudy
*
* #param string $studyWishPlaceStudy
*
* #return Candidat
*/
public function setStudyWishPlaceStudy($studyWishPlaceStudy)
{
$this->studyWishPlaceStudy = $studyWishPlaceStudy;
return $this;
}
/**
* Get studyWishPlaceStudy
*
* #return string
*/
public function getStudyWishPlaceStudy()
{
return $this->studyWishPlaceStudy;
}
/**
* Set user
*
* #param guid $user
*
* #return \UserBundle\Entity\User $user
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set studyWishSpecialities
*
* #param \SchoolBundle\Entity\Speciality $studyWishSpecialities
*
* #return Candidat
*/
public function setStudyWishSpecialities($studyWishSpecialities)
{
$this->studyWishSpecialities = $studyWishSpecialities;
return $this;
}
/**
* Get studyWishSpecialities
*
* #return \SchoolBundle\Entity\Speciality
*/
public function getStudyWishSpecialities()
{
return $this->studyWishSpecialities;
}
public function setPicture(Attachment $picture = null)
{
$this->picture = $picture;
}
public function getPicture()
{
return $this->picture;
}
public function setCv(Attachment $cv = null)
{
$this->cv = $cv;
}
public function getCv()
{
return $this->cv;
}
/**
* Add attachment
*
* #param \CandidacyBundle\Entity\Attachment $attachment
*
* #return Candidat
*/
public function addAttachment(\CandidacyBundle\Entity\Attachment $attachment)
{
$this->attachments[] = $attachment;
return $this;
}
/**
* Remove attachment
*
* #param \CandidacyBundle\Entity\Attachment $attachment
*/
public function removeAttachment(\CandidacyBundle\Entity\Attachment $attachment)
{
$this->attachments->removeElement($attachment);
}
/**
* Get attachments
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getAttachments()
{
return $this->attachments;
}
/**
* Set school
*
* #param \SchoolBundle\Entity\School $school
*
* #return Candidat
*/
public function setSchool(\SchoolBundle\Entity\School $school = null)
{
$this->school = $school;
return $this;
}
/**
* Get school
*
* #return \SchoolBundle\Entity\School
*/
public function getSchool()
{
return $this->school;
}
/**
* Add studyWishSpeciality
*
* #param \SchoolBundle\Entity\Speciality $studyWishSpeciality
*
* #return Candidat
*/
public function addStudyWishSpeciality(\SchoolBundle\Entity\Speciality $studyWishSpeciality)
{
$this->studyWishSpecialities[] = $studyWishSpeciality;
return $this;
}
/**
* Remove studyWishSpeciality
*
* #param \SchoolBundle\Entity\Speciality $studyWishSpeciality
*/
public function removeStudyWishSpeciality(\SchoolBundle\Entity\Speciality $studyWishSpeciality)
{
$this->studyWishSpecialities->removeElement($studyWishSpeciality);
}
/**
* Set studyWishSection
*
* #param \SchoolBundle\Entity\Section $studyWishSection
*
* #return Candidat
*/
public function setStudyWishSection(\SchoolBundle\Entity\Section $studyWishSection = null)
{
$this->studyWishSection = $studyWishSection;
return $this;
}
/**
* Get studyWishSection
*
* #return \SchoolBundle\Entity\Section
*/
public function getStudyWishSection()
{
return $this->studyWishSection;
}
/**
* Add studyWishDiploma
*
* #param \SchoolBundle\Entity\Diploma $studyWishDiploma
*
* #return Candidat
*/
public function addStudyWishDiploma(\SchoolBundle\Entity\Diploma $studyWishDiploma)
{
$this->studyWishDiploma[] = $studyWishDiploma;
return $this;
}
/**
* Remove studyWishDiploma
*
* #param \SchoolBundle\Entity\Diploma $studyWishDiploma
*/
public function removeStudyWishDiploma(\SchoolBundle\Entity\Diploma $studyWishDiploma)
{
$this->studyWishDiploma->removeElement($studyWishDiploma);
}
/**
* Get studyWishDiploma
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getStudyWishDiploma()
{
return $this->studyWishDiploma;
}
/**
* Get studyWishDiplomas
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getStudyWishDiplomas()
{
return $this->studyWishDiplomas;
}
}

Related

Symfony2 form with multiple entities slow on submit

I have an entity called Materials with 8 associated entities (with join tables).
When I submit the form to update existing entities/records in the database, it takes up to 24 seconds to finish the process. I read somewhere that I shouldn't use:
$em = $this->getDoctrine()->getManager();
$form->handleRequest($request);
$data = $form->getData();
$em->persist($data);
$em->flush();
Because multiple entities would take too long to persist, but to boost performance I should use the update query:
Exp.
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('ourcodeworldBundle:Posts');
$newCreatedAt = new \DateTime();
$qb = $repo->createQueryBuilder('p');
$qb->update()
->set('p.createdAt', ':newCreatedAt')
->setParameter('newCreatedAt', $newCreatedAt);
$qb->getQuery()->execute();
https://ourcodeworld.com/articles/read/2/5-simple-tips-for-boost-the-database-handling-with-symfony2-and-doctrine
Does anybody know if that is correct and if so, will I have to really update every entity manually? It will take me a long time to write this as queries, since there are so many.
I found the problem.
The form used to fail at $form->handleRequest($request); in my controller:
if ($request->getMethod() == 'POST') {
$req = $request->request->get("material");
$form->handleRequest($request);
...
}
But the real problem was that my Assert statements in my Material class were not well defined. As soon as I corrected them handleRequest no longer slowed down the form submission process.
Here is my updated Material class with the fixed Assert statements:
/**
* #ORM\Table(name="materials")
* #ORM\Entity(repositoryClass="AppBundle\Entity\MaterialRepository")
*/
class Material
{
/**
* #var integer
*
* #ORM\Column(name="materialID", type="integer", nullable=true)
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $materialID;
/**
* #var integer
*
* #Assert\Type("\integer")
* #ORM\Column(name="lrcID", type="integer", nullable=true)
*/
private $lrcID;
/**
* #var string
*
* #Assert\Type("\string")
* #Assert\Length(max=255)
* #ORM\Column(name="title", type="string", length=255, nullable=false)
*/
private $title;
/**
* #var string
*
* #Assert\Type("\string")
* #ORM\Column(name="description", type="text", nullable=false)
*/
private $description;
/**
* #var string
*
* #Assert\Type("\string")
* #Assert\Length(max=255)
* #ORM\Column(name="author", type="string", length=255, nullable=true)
*/
private $author;
/**
* #var integer
*
* #Assert\Type("\integer")
* #ORM\Column(name="year", type="integer", nullable=true)
*/
private $year;
/**
* #var File
*
* #Assert\File(
* maxSize = "10M",
* mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
* maxSizeMessage = "The maxmimum allowed file size is 10MB.",
* mimeTypesMessage = "Please, upload the imag as a jpge, gif, png, or tiff file."
* )
* #ORM\Column(name="image", type="string", length=100, nullable=true)
*/
private $image;
/**
* #var \DateTime
*
* #Assert\NotBlank()
* #Assert\Type("\DateTime")
* #ORM\Column(name="dateModified", type="datetime", nullable=true)
*/
private $dateModified;
/**
* #var integer
*
* #Assert\Type("\integer")
* #ORM\Column(name="isActive", type="integer", nullable=true)
*/
private $isActive;
/**
* #var integer
*
* #Assert\Type("\integer")
* #ORM\Column(name="isFree", type="integer", nullable=true)
*/
private $isFree;
/**
* #var integer
*
* #Assert\Type("\integer")
* #ORM\Column(name="sizevalue", type="integer", nullable=true)
*/
private $sizevalue;
/**
* #var integer
*
* #Assert\Type("\integer")
* #ORM\Column(name="sizeunit", type="integer", nullable=true)
*/
private $sizeunit;
/**
* #var integer
*
* #Assert\Type("\integer")
* #ORM\Column(name="isComplete", type="integer", nullable=true)
*/
private $isComplete;
/**
*
* #Assert\Url(
* checkDNS = true,
* message = "The url '{{ value }}' is not a valid url",
* dnsMessage = "The host '{{ value }}' could not be resolved.",
* )
* #Assert\Length(max=255)
* #ORM\Column(name="url", type="string", length=255, nullable=false)
*/
private $url;
/**
* #Assert\Type(type="AppBundle\Entity\MaterialLanguage")
* #Assert\Valid()
* #ORM\ManyToMany(targetEntity="MaterialLanguage", inversedBy="material")
* #ORM\JoinTable(name="materials_language_map",
* joinColumns={#ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="languageID", referencedColumnName="languageID", nullable=false)})
*/
public $materiallanguage;
/**
* #Assert\Type(type="AppBundle\Entity\MaterialType")
* #Assert\Valid()
* #ORM\ManyToMany(targetEntity="MaterialType", inversedBy="material")
* #ORM\JoinTable(name="materials_type_map",
* joinColumns={#ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="typeID", referencedColumnName="typeID", nullable=false)})
*/
public $materialtype;
/**
* #Assert\Type(type="AppBundle\Entity\MaterialAudience")
* #Assert\Valid()
* #ORM\ManyToMany(targetEntity="MaterialAudience", inversedBy="material")
* #ORM\JoinTable(name="materials_audience_map",
* joinColumns={#ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="audienceID", referencedColumnName="audienceID", nullable=false)})
*/
public $materialaudience;
/**
* #Assert\Type(type="AppBundle\Entity\MaterialLevel")
* #Assert\Valid()
* #ORM\ManyToMany(targetEntity="MaterialLevel", inversedBy="material")
* #ORM\JoinTable(name="materials_level_map",
* joinColumns={#ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="levelID", referencedColumnName="levelID", nullable=false)})
*/
public $materiallevel;
/**
* #Assert\Type(type="AppBundle\Entity\MaterialFormat")
* #Assert\Valid()
* #ORM\ManyToMany(targetEntity="MaterialFormat")
* #ORM\JoinTable(name="materials_format_map",
* joinColumns={#ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="formatid", referencedColumnName="formatid", nullable=false)})
*/
public $materialformat;
/**
* #Assert\Type(type="AppBundle\Entity\MaterialSpecificMedium")
* #Assert\Valid()
* #ORM\ManyToMany(targetEntity="MaterialSpecificMedium")
* #ORM\JoinTable(name="materials_specificmedium_map",
* joinColumns={#ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="specificmediumID", referencedColumnName="specificmediumid", nullable=false)})
*/
public $materialspecificmedium;
/**
* #Assert\Type(type="AppBundle\Entity\MaterialSizeUnits")
* #Assert\Valid()
* #ORM\ManyToOne(targetEntity="MaterialSizeUnits", inversedBy="material")
* #ORM\JoinColumn(name="sizeunit", referencedColumnName="id", nullable=true)
*/
public $materialsizeunits;
/**
* #Assert\Type(type="AppBundle\Entity\MaterialCategory")
* #Assert\Valid()
* #ORM\ManyToMany(targetEntity="MaterialCategory")
* #ORM\JoinTable(name="materials_category_map",
* joinColumns={#ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="categoryID", referencedColumnName="categoryID", nullable=false)})
*/
public $materialcategory;
/**
* #Assert\Type(type="AppBundle\Entity\MaterialKeyword")
* #Assert\Valid()
* #ORM\ManyToMany(targetEntity="MaterialKeyword", inversedBy="material")
* #ORM\JoinTable(name="materials_keyword_map",
* joinColumns={#ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="keywordID", referencedColumnName="id", nullable=false)})
*/
public $materialkeyword;
/**
* #Assert\Type(type="AppBundle\Entity\MaterialYear")
* #Assert\Valid()
* #ORM\ManyToOne(targetEntity="MaterialYear")
* #ORM\JoinColumn(name="year", referencedColumnName="yearID")
*/
public $materialyear;
/**
* #Assert\Type(type="AppBundle\Entity\Lrc")
* #Assert\Valid()
* #ORM\ManyToOne(targetEntity="Lrc", inversedBy="material")
* #ORM\JoinColumn(name="lrcID", referencedColumnName="id")
*/
public $lrc;
/**
* Constructor
*/
public function __construct()
{
$this->MaterialLanguage = new ArrayCollection();
$this->MaterialType = new ArrayCollection();
$this->MaterialLevel = new ArrayCollection();
$this->MaterialAudience = new ArrayCollection();
$this->MaterialFormat = new ArrayCollection();
$this->MaterialSpecificMedium = new ArrayCollection();
$this->MaterialSizeUnits = new ArrayCollection();
$this->MaterialCategory = new ArrayCollection();
$this->MaterialKeyword = new ArrayCollection();
$this->MaterialYear = new ArrayCollection();
$this->Lrc = new ArrayCollection();
}
/**
* Set materiallanguage
*
* #param array $materiallanguage
*
*/
public function setMateriallanguage(ArrayCollection $materiallanguage)
{
$this->materiallanguage = $materiallanguage;
}
/**
* Get materiallanguage
*
* #Assert\Type("\array")
* #return array
*/
public function getMateriallanguage()
{
return $this->materiallanguage;
}
/**
* Set materialtype
*
* #param array $materialtype
*
*/
public function setMaterialtype(ArrayCollection $materialtype)
{
$this->materialtype = $materialtype;
}
/**
* Get materialtype
*
* #Assert\Type("\array")
* #return array
*/
public function getMaterialtype()
{
return $this->materialtype;
}
/**
* Set materialaudience
*
* #param array $materialaudience
*
*/
public function setMaterialaudience(ArrayCollection $materialaudience)
{
$this->materialaudience = $materialaudience;
}
/**
* Get materialaudience
*
* #Assert\Type("\array")
* #return array
*/
public function getMaterialaudience()
{
return $this->materialaudience;
}
/**
* Set materialformat
*
* #param array $materialformat
*
*/
public function setMaterialformat(ArrayCollection $materialformat)
{
$this->materialformat = $materialformat;
}
/**
* Get materialformat
*
* #Assert\Type("\array")
* #return array
*/
public function getMaterialformat()
{
return $this->materialformat;
}
/**
* Set materialspecificmedium
*
* #param array $materialspecificmedium
*
*/
public function setMaterialspecificmedium(ArrayCollection $materialspecificmedium)
{
$this->materialspecificmedium = $materialspecificmedium;
}
/**
* Get materialspecificmedium
*
* #Assert\Type("\array")
* #return array
*/
public function getMaterialspecificmedium()
{
return $this->materialspecificmedium;
}
/**
* Set materiallevel
*
* #param array $materiallevel
*
*/
public function setMateriallevel(ArrayCollection $materiallevel)
{
$this->materiallevel = $materiallevel;
}
/**
* Get materiallevel
*
* #Assert\Type("\array")
* #return array
*/
public function getMateriallevel()
{
return $this->materiallevel;
}
/**
* Set materialsizeunits
*
* #param array $materialsizeunits
*
*/
public function setMaterialsizeunits(MaterialSizeUnits $materialsizeunits)
{
$this->materialsizeunits = $materialsizeunits;
}
/**
* Get materialsizeunits
*
* #return array
*/
public function getMaterialsizeunits()
{
return $this->materialsizeunits;
}
/**
* Set materialcategory
*
* #param array $materialcategory
*
*/
public function setMaterialcategory(ArrayCollection $materialcategory)
{
$this->materialcategory = $materialcategory;
}
/**
* Get materialcategory
*
* #Assert\Type("\array")
* #return array
*/
public function getMaterialcategory()
{
return $this->materialcategory;
}
/**
* Set materialkeyword
*
* #param array $materialkeyword
*
*/
public function setMaterialkeyword(MaterialKeyword $materialkeyword)
{
$this->materialkeyword = $materialkeyword;
}
/**
* Get materialkeyword
*
* #Assert\Type("\array")
* #return array
*/
public function getMaterialkeyword()
{
return $this->materialkeyword;
}
/**
* Set materialyear
*
* #param array $materialyear
*
*/
public function setMaterialyear(MaterialYear $materialyear)
{
$this->materialyear = $materialyear;
}
/**
* Get materiallamaterialyear
*
* #Assert\Type("\array")
* #return array
*/
public function getMaterialyear()
{
return $this->materialyear;
}
/**
* Set lrc
*
* #param array $lrc
*
*/
public function setLrc(Lrc $lrc=null)
{
$this->lrc = $lrc;
}
/**
* Get lrc
*
* #Assert\Type("\array")
* #return array
*/
public function getLrc()
{
return $this->lrc;
}
/**
* Set materialID
*
* #param integer $materialID
*
* #return Material
*/
public function setMaterialID($materialID)
{
$this->materialID = $materialID;
return $this;
}
/**
* Get materialID
*
* #return integer
*/
public function getMaterialID()
{
return $this->materialID;
}
/**
* Set lrcID
*
* #param integer $lrcID
*
* #return Material
*/
public function setLrcID($lrcID)
{
$this->lrcID = $lrcID;
return $this;
}
/**
* Get lrcID
*
* #return integer
*/
public function getLrcID()
{
return $this->lrcID;
}
/**
* Set title
*
* #param string $title
*
* #return Material
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* #param string $description
*
* #return Material
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set author
*
* #param string $author
*
* #return Material
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set year
*
* #param integer $year
*
* #return Material
*/
public function setYear($year)
{
$this->year = $year;
return $this;
}
/**
* Get year
*
* #return integer
*/
public function getYear()
{
return $this->year;
}
/**
* Set image
*
* #param string $image
*
* #return Material
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* #return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set dateModified
*
* #param \DateTime $dateModified
*
* #return Material
*/
public function setDateModified(\DateTime $dateModified)
{
$this->dateModified = $dateModified;
return $this;
}
/**
* Get dateModified
*
* #return \DateTime
*/
public function getDateModified()
{
return $this->dateModified;
}
/**
* Set isActive
*
* #param integer $isActive
*
* #return Material
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* #return integer
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set isFree
*
* #param integer $isFree
*
* #return Material
*/
public function setIsFree($isFree)
{
$this->isFree = $isFree;
return $this;
}
/**
* Get isFree
*
* #return integer
*/
public function getIsFree()
{
return $this->isFree;
}
/**
* Set sizevalue
*
* #param integer $sizevalue
*
*/
public function setSizevalue($sizevalue)
{
$this->sizevalue = $sizevalue;
}
/**
* Get sizevalue
*
* #return integer
*/
public function getSizevalue()
{
return $this->sizevalue;
}
/**
* Set sizeunit
*
* #param integer $sizeunit
*
* #return Material
*/
public function setSizeunit($sizeunit)
{
$this->sizeunit = $sizeunit;
return $this;
}
/**
* Get sizeunit
*
* #return integer
*/
public function getSizeunit()
{
return $this->sizeunit;
}
/**
* Set isComplete
*
* #param integer $isComplete
*
* #return Material
*/
public function setIsComplete($isComplete)
{
$this->isComplete = $isComplete;
return $this;
}
/**
* Get isComplete
*
* #return integer
*/
public function getIsComplete()
{
return $this->isComplete;
}
/**
* Set url
*
* #param string $url
*
* #return Material
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* #return string
*/
public function getUrl()
{
return $this->url;
}
}

Symfony / Doctrine - Id already exist

I have a problem. I moved website to another hosting(so I exported DB and imported to new hosting).
But now, when I trying to add new record to database. I have an error stn like ID 1 already exist. But I have almost 500 records in the table.
What can I do to fix it?
Sorry I've should provide more information:
I exported and imported database through phppgadmin I didn't use migrations bundle.
Here is my entity:
class Products
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="serial_number", type="string", length=255, nullable=true)
*/
private $serial;
/**
* #var string
* #Expose
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
* #Expose
* #ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* #var string
* #ORM\Column(name="note", type="text", nullable=true)
*/
private $note;
/**
* #var int
*
* #ORM\Column(name="views", type="bigint", nullable=true)
*/
private $views;
/**
* #ORM\ManyToMany(targetEntity="Models")
* #ORM\JoinTable(name="products_models",
* joinColumns={#ORM\JoinColumn(name="product_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="model_id", referencedColumnName="id")}
* )
*/
private $models;
/**
* #ORM\OneToOne(targetEntity="ProductDetails", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="details_id", referencedColumnName="id")
*/
private $details;
/**
* #var File
* #Expose
* #ORM\OneToMany(targetEntity="ProductImages", mappedBy="product", cascade={"persist", "remove"})
* #ORM\OrderBy({"id" = "ASC"})
*
*/
private $images;
/**
* #var File
*
* #ORM\OneToMany(targetEntity="Cart", mappedBy="productId", cascade={"persist"})
*
*/
private $cart;
/**
* #var string
*
* #ORM\Column(name="price", type="integer", length=255, nullable=true)
*/
private $price;
/**
* #var string
*
* #ORM\Column(name="bought_price", type="integer", length=255, nullable=true)
*/
private $boughtPrice;
/**
* #var string
*
* #ORM\Column(name="old_price", type="integer", length=255, nullable=true)
*/
private $oldPrice;
/**
* #var bool
*
* #ORM\Column(name="is_active", type="boolean", nullable=true)
*/
private $isActive;
/**
* #var bool
*
* #ORM\Column(name="is_accessory", type="boolean", nullable=true)
*/
private $isAccessory;
/**
* #ORM\ManyToOne(targetEntity="AccessoryCategory")
* #ORM\JoinColumn(name="accessory_category_id", referencedColumnName="id")
*/
private $accessoryCategory;
/**
* #var bool
*
* #ORM\Column(name="is_special", type="boolean", nullable=true)
*/
private $isSpecial;
/**
* #var integer
*
* #ORM\Column(name="quantity", type="integer", nullable=true)
*/
private $quantity;
/**
* created Time/Date
*
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
protected $createdAt;
/**
* updated Time/Date
*
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
protected $updatedAt;
/**
* #var boolean
*
* #ORM\Column(name="seller", type="boolean", length=255, nullable=true)
*/
private $seller;
/**
* Set createdAt
*
* #ORM\PrePersist
*/
public function setCreatedAt()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #ORM\PreUpdate
*/
public function setUpdatedAt()
{
$this->updatedAt = new \DateTime();
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Products
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set url
*
* #param string $url
*
* #return Products
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* #return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set note
*
* #param string $note
*
* #return Products
*/
public function setNote($note)
{
$this->note = $note;
return $this;
}
/**
* Get note
*
* #return string
*/
public function getNote()
{
return $this->note;
}
/**
* Set views
*
* #param integer $views
*
* #return Products
*/
public function setViews($views)
{
$this->views = $views;
return $this;
}
/**
* Get views
*
* #return integer
*/
public function getViews()
{
return $this->views;
}
/**
* Set price
*
* #param integer $price
*
* #return Products
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* #return integer
*/
public function getPrice()
{
return $this->price;
}
/**
* Set boughtPrice
*
* #param integer $boughtPrice
*
* #return Products
*/
public function setBoughtPrice($boughtPrice)
{
$this->boughtPrice = $boughtPrice;
return $this;
}
/**
* Get boughtPrice
*
* #return integer
*/
public function getBoughtPrice()
{
return $this->boughtPrice;
}
/**
* Set isActive
*
* #param boolean $isActive
*
* #return Products
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* #return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set isAccessory
*
* #param boolean $isAccessory
*
* #return Products
*/
public function setIsAccessory($isAccessory)
{
$this->isAccessory = $isAccessory;
return $this;
}
/**
* Get isAccessory
*
* #return boolean
*/
public function getIsAccessory()
{
return $this->isAccessory;
}
/**
* Set quantity
*
* #param integer $quantity
*
* #return Products
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* #return integer
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Set details
*
* #param \Web\AdminBundle\Entity\ProductDetails $details
*
* #return Products
*/
public function setDetails(\Web\AdminBundle\Entity\ProductDetails $details = null)
{
$this->details = $details;
return $this;
}
/**
* Get details
*
* #return \Web\AdminBundle\Entity\ProductDetails
*/
public function getDetails()
{
return $this->details;
}
/**
* Add image
*
* #param \Web\AdminBundle\Entity\ProductImages $image
*
* #return Products
*/
public function addImage(\Web\AdminBundle\Entity\ProductImages $image)
{
$this->images[] = $image;
return $this;
}
/**
* Remove image
*
* #param \Web\AdminBundle\Entity\ProductImages $image
*/
public function removeImage(\Web\AdminBundle\Entity\ProductImages $image)
{
$this->images->removeElement($image);
}
/**
* Get images
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
/**
* Add cart
*
* #param \Web\AdminBundle\Entity\Cart $cart
*
* #return Products
*/
public function addCart(\Web\AdminBundle\Entity\Cart $cart)
{
$this->cart[] = $cart;
return $this;
}
/**
* Remove cart
*
* #param \Web\AdminBundle\Entity\Cart $cart
*/
public function removeCart(\Web\AdminBundle\Entity\Cart $cart)
{
$this->cart->removeElement($cart);
}
/**
* Get cart
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCart()
{
return $this->cart;
}
/**
* Set isSpecial
*
* #param boolean $isSpecial
*
* #return Products
*/
public function setIsSpecial($isSpecial)
{
$this->isSpecial = $isSpecial;
return $this;
}
/**
* Get isSpecial
*
* #return boolean
*/
public function getIsSpecial()
{
return $this->isSpecial;
}
/**
* Set accessoryCategory
*
* #param \Web\AdminBundle\Entity\AccessoryCategory $accessoryCategory
*
* #return Products
*/
public function setAccessoryCategory(\Web\AdminBundle\Entity\AccessoryCategory $accessoryCategory = null)
{
$this->accessoryCategory = $accessoryCategory;
return $this;
}
/**
* Get accessoryCategory
*
* #return \Web\AdminBundle\Entity\AccessoryCategory
*/
public function getAccessoryCategory()
{
return $this->accessoryCategory;
}
/**
* Set oldPrice
*
* #param integer $oldPrice
*
* #return Products
*/
public function setOldPrice($oldPrice)
{
$this->oldPrice = $oldPrice;
return $this;
}
/**
* Get oldPrice
*
* #return integer
*/
public function getOldPrice()
{
return $this->oldPrice;
}
/**
* Set serial
*
* #param string $serial
*
* #return Products
*/
public function setSerial($serial)
{
$this->serial = $serial;
return $this;
}
/**
* Get serial
*
* #return string
*/
public function getSerial()
{
return $this->serial;
}
/**
* Constructor
*/
public function __construct()
{
$this->models = new \Doctrine\Common\Collections\ArrayCollection();
$this->images = new \Doctrine\Common\Collections\ArrayCollection();
$this->cart = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add model
*
* #param \Web\AdminBundle\Entity\Models $model
*
* #return Products
*/
public function addModel(\Web\AdminBundle\Entity\Models $model)
{
$this->models[] = $model;
return $this;
}
/**
* Remove model
*
* #param \Web\AdminBundle\Entity\Models $model
*/
public function removeModel(\Web\AdminBundle\Entity\Models $model)
{
$this->models->removeElement($model);
}
/**
* Get models
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getModels()
{
return $this->models;
}
/**
* Set seller
*
* #param boolean $seller
*
* #return Products
*/
public function setSeller($seller)
{
$this->seller = $seller;
return $this;
}
/**
* Get seller
*
* #return boolean
*/
public function getSeller()
{
return $this->seller;
}
I'm adding new like this:
$em = $this->getDoctrine()->getManager();
$products = new Products();
$article->setTitle('here is the title');
....
$em->persist($products);
$em->flush();
this should set id to like 488 but it's trying to set it to 1. And I don't know why? Might be some cache? But php bin/console cache:clear doesn't change anything.
Doctrine use sequence with the auto strategy and Postgres.
Maybe sequence values has been lost when export/import db.
Identify the sequence used by your ID and try to execute :
ALTER SEQUENCE sequence_name RESTART WITH your_next_free_id;
hi i have got the same problem when i Import my database the probléme s like they say sequence.
But because i have more than 100 table i can't reset the sequence one by one So i Found this query it create for all your table an sql query to update the sequence by the max Id, you have just to copy the result and execute it
SELECT 'SELECT SETVAL(' ||
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
FROM pg_class AS S,
pg_depend AS D,
pg_class AS T,
pg_attribute AS C,
pg_tables AS PGT
WHERE S.relkind = 'S'
AND S.oid = D.objid
AND D.refobjid = T.oid
AND D.refobjid = C.attrelid
AND D.refobjsubid = C.attnum
AND T.relname = PGT.tablename
ORDER BY S.relname;
I think as other have suggested its not the issue with symfony or doctrine , instead its postgresql common issue, you can check a solution here
Make sure you are using same version, I am not sure if this behavior differs from version to version.
You can change the current value of the auto increment manually like this :
ALTER TABLE products AUTO_INCREMENT=501;

Catchable Fatal Error: Object of class AppBundle\Entity\Categoria could not be converted to string

Produto.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Produto
*
* #ORM\Table(name="produto")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ProdutoRepository")
*/
class Produto
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="descricao", type="string", length=50)
*/
private $descricao;
/**
* #ORM\ManyToOne(targetEntity="Categoria", inversedBy="produtos")
* #ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
*/
private $categoria;
/**
* #var decimal
*
* #ORM\Column(name="valor", type="decimal", scale=2)
*/
private $valor;
/**
* #var int
*
* #ORM\Column(name="multiplo", type="integer")
*/
private $multiplo;
/**
* #var int
*
* #ORM\Column(name="etapa", type="integer")
*/
private $etapa;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set descricao
*
* #param string $descricao
*
* #return Produto
*/
public function setDescricao($descricao)
{
$this->descricao = $descricao;
return $this;
}
/**
* Get descricao
*
* #return string
*/
public function getDescricao()
{
return $this->descricao;
}
/**
* Set categoria
*
* #param \AppBundle\Entity\Categoria $categoria
*
* #return Produto
*/
public function setCategoria(\AppBundle\Entity\Categoria $categoria = null)
{
$this->categoria = $categoria;
}
/**
* Get categoria
*
* #return \AppBundle\Entity\Categoria
*/
public function getCategoria()
{
return $this->categoria;
}
/**
* Set valor
*
* #param string $valor
*
* #return Produto
*/
public function setValor($valor)
{
$this->valor = $valor;
return $this;
}
/**
* Get valor
*
* #return string
*/
public function getValor()
{
return $this->valor;
}
/**
* Set multiplo
*
* #param \int $multiplo
*
* #return Produto
*/
public function setMultiplo($multiplo)
{
$this->multiplo = $multiplo;
return $this;
}
/**
* Get multiplo
*
* #return \int
*/
public function getMultiplo()
{
return $this->multiplo;
}
/**
* Set etapa
*
* #param \int $etapa
*
* #return Produto
*/
public function setEtapa( $etapa)
{
$this->etapa = $etapa;
return $this;
}
/**
* Get etapa
*
* #return \int
*/
public function getEtapa()
{
return $this->etapa;
}
}
Categoria.php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Categoria
*
* #ORM\Table(name="categoria")
* #ORM\Entity(repositoryClass="AppBundle\Repository\CategoriaRepository")
*/
class Categoria
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="descricao", type="string", length=50)
*/
private $descricao;
/**
* #ORM\OneToMany(targetEntity="Produto", mappedBy="categoria")
*/
private $produtos;
/**
* #ORM\OneToMany(targetEntity="Subcategoria", mappedBy="categoria")
*/
private $subcategorias;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set descricao
*
* #param string $descricao
*
* #return Categoria
*/
public function setDescricao($descricao)
{
$this->descricao = $descricao;
return $this;
}
/**
* Get descricao
*
* #return string
*/
public function getDescricao()
{
return $this->descricao;
}
public function __construct(){
$this->produtos = new ArrayCollection();
$this->subcategorias = new ArrayCollection();
}
/**
* Add produto
*
* #param \AppBundle\Entity\Produto $produto
*
* #return Categoria
*/
public function addProduto(\AppBundle\Entity\Produto $produto)
{
$this->produtos[] = $produto;
return $this;
}
/**
* Remove produto
*
* #param \AppBundle\Entity\Produto $produto
*/
public function removeProduto(\AppBundle\Entity\Produto $produto)
{
$this->produtos->removeElement($produto);
}
/**
* Get produtos
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getProdutos()
{
return $this->produtos;
}
/**
* Add subcategoria
*
* #param \AppBundle\Entity\Subcategoria $subcategoria
*
* #return Categoria
*/
public function addSubcategoria(\AppBundle\Entity\Subcategoria $subcategoria)
{
$this->subcategorias[] = $subcategoria;
return $this;
}
/**
* Remove subcategoria
*
* #param \AppBundle\Entity\Subcategoria $subcategoria
*/
public function removeSubcategoria(\AppBundle\Entity\Subcategoria $subcategoria)
{
$this->subcategorias->removeElement($subcategoria);
}
/**
* Get subcategorias
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getSubcategorias()
{
return $this->subcategorias;
}
}
How can fix it?
Try adding a method called "__toString()" in AppBundle/Entity/Categoria, and return a string to identify your object:
/**
* #return string
*/
public function __toString()
{
return $this->title;
}

A2lix TranslationFormBundle - Gedmo Doctrine Extension - Unwanted form fields

I am trying to build forms for translatable entities but i get 4 form fields that i don't want.
It seems these fields are coming from the Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation but i wonder what i need to do with these fields ?
Is it normal that they are appearing in the translation form ?
If someone have usefull examples that would be great.
It's a Symfony2 project.
Doctrine extensions are installed with stof:
https://github.com/stof/StofDoctrineExtensionsBundle/
Aj2lix TranslationFormBundle:
https://github.com/a2lix/TranslationFormBundle
Please find below the following files:
Page.php (this is the main entity)
PageTranslation.php (the translation entity)
Page.php
<?php
namespace Syms\PageBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Page
*
* #Gedmo\Tree(type="nested")
* #ORM\Table(name="syms_page")
* #ORM\Entity(repositoryClass="Syms\PageBundle\Entity\PageRepository")
*/
class Page
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var boolean
*
* #ORM\Column(name="is_active", type="boolean", nullable=false, options={"default":0})
*/
private $isActive;
/**
* #var boolean
*
* #ORM\Column(name="is_homepage", type="boolean", nullable=false, options={"default":0})
*/
private $isHomepage;
/**
* #var \DateTime
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
protected $translations;
/**
* #Gedmo\TreeLeft
* #ORM\Column(name="lft", type="integer")
*/
private $lft;
/**
* #Gedmo\TreeRight
* #ORM\Column(name="rgt", type="integer")
*/
private $rgt;
/**
* #Gedmo\TreeLevel
* #ORM\Column(name="lvl", type="integer")
*/
private $lvl;
/**
* #Gedmo\TreeParent
* #ORM\ManyToOne(targetEntity="Page", inversedBy="children")
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* #Gedmo\TreeRoot
* #ORM\Column(name="root", type="integer", nullable=true)
*/
private $root;
/**
* #ORM\OneToMany(targetEntity="Page", mappedBy="parent")
* #ORM\OrderBy({"lft" = "ASC"})
*/
private $children;
/**
* Required for Translatable behaviour
* #Gedmo\Locale
*/
protected $locale;
/**
* Constructor
*/
public function __construct()
{
$this->children = new ArrayCollection();
$this->translations = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set isActive
*
* #param boolean $isActive
* #return Page
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* #return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set isHomepage
*
* #param boolean $isHomepage
* #return Page
*/
public function setIsHomepage($isHomepage)
{
$this->isHomepage = $isHomepage;
return $this;
}
/**
* Get isHomepage
*
* #return boolean
*/
public function getIsHomepage()
{
return $this->isHomepage;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Page
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
public function setParent(Page $parent)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
/**
* Set lft
*
* #param integer $lft
* #return Page
*/
public function setLft($lft)
{
$this->lft = $lft;
return $this;
}
/**
* Get lft
*
* #return integer
*/
public function getLft()
{
return $this->lft;
}
/**
* Set rgt
*
* #param integer $rgt
* #return Page
*/
public function setRgt($rgt)
{
$this->rgt = $rgt;
return $this;
}
/**
* Get rgt
*
* #return integer
*/
public function getRgt()
{
return $this->rgt;
}
/**
* Set lvl
*
* #param integer $lvl
* #return Page
*/
public function setLvl($lvl)
{
$this->lvl = $lvl;
return $this;
}
/**
* Get lvl
*
* #return integer
*/
public function getLvl()
{
return $this->lvl;
}
/**
* Set root
*
* #param integer $root
* #return Page
*/
public function setRoot($root)
{
$this->root = $root;
return $this;
}
/**
* Get root
*
* #return integer
*/
public function getRoot()
{
return $this->root;
}
/**
* Add children
*
* #param \Syms\PageBundle\Entity\Page $children
* #return Page
*/
public function addChild(Page $children)
{
$this->children[] = $children;
return $this;
}
/**
* Remove children
*
* #param \Syms\PageBundle\Entity\Page $children
*/
public function removeChild(Page $children)
{
$this->children->removeElement($children);
}
/**
* Get children
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
public function getTranslations()
{
return $this->translations;
}
public function addTranslation(PageTranslation $t)
{
$this->translations->add($t);
$t->setObject($this);
}
public function removeTranslation(PageTranslation $t)
{
$this->translations->removeElement($t);
}
public function setTranslations($translations)
{
$this->translations = $translations;
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
}
PageTranslation.php
<?php
namespace Syms\PageBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Table(name="ext_translations_page", indexes={
* #ORM\Index(name="page_translation_idx", columns={"locale", "object_class", "field", "foreign_key"})
* })
* #ORM\Entity(repositoryClass="Gedmo\Translatable\Entity\Repository\TranslationRepository")
*/
class PageTranslation extends AbstractTranslation
{
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(name="meta_title", type="string", length=255)
*/
private $metaTitle;
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(name="meta_keywords", type="text", nullable=true)
*/
private $metaKeywords;
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(name="meta_description", type="text", nullable=true)
*/
private $metaDescription;
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(name="url_key", type="string", length=255)
*/
private $urlKey;
/**
* Set title
*
* #param string $title
* #return PageTranslation
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set metaTitle
*
* #param string $metaTitle
* #return PageTranslation
*/
public function setMetaTitle($metaTitle)
{
$this->metaTitle = $metaTitle;
return $this;
}
/**
* Get metaTitle
*
* #return string
*/
public function getMetaTitle()
{
return $this->metaTitle;
}
/**
* Set metaKeywords
*
* #param string $metaKeywords
* #return PageTranslation
*/
public function setMetaKeywords($metaKeywords)
{
$this->metaKeywords = $metaKeywords;
return $this;
}
/**
* Get metaKeywords
*
* #return string
*/
public function getMetaKeywords()
{
return $this->metaKeywords;
}
/**
* Set metaDescription
*
* #param string $metaDescription
* #return PageTranslation
*/
public function setMetaDescription($metaDescription)
{
$this->metaDescription = $metaDescription;
return $this;
}
/**
* Get metaDescription
*
* #return string
*/
public function getMetaDescription()
{
return $this->metaDescription;
}
/**
* Set urlKey
*
* #param string $urlKey
* #return PageTranslation
*/
public function setUrlKey($urlKey)
{
$this->urlKey = $urlKey;
return $this;
}
/**
* Get urlKey
*
* #return string
*/
public function getUrlKey()
{
return $this->urlKey;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
I've finally succeeded in displaying the correct fields.
You have to downgrade the a2lix form bundle to version 1.4.
In fact, in version 2.0, the field type "a2lix_translations_gedmo" is no more available, which is actually what you need when using Gedmo Translatable.
There's nothing to change on your entity side. Just follow the doc for the config.yml, and use "a2lix_translations_gedmo" in your PageType :
$builder->add('translations', 'a2lix_translations_gedmo', array(
'translatable_class' => "Syms\PageBundle\Entity\Page"
);
I've also red somewhere that you can keep using version 2.0 of a2lix Form Bundle, but in this case you have to use a specific branch of doctrine extensions (wip-v2.4.0).
Regards,

Symfony2 - Able to add data of multiple entities to database but no foreign keys are added

I got 2 tables connected with a foreign key (user_id is in the table persons). I'm able to add data to both of the tables by executing 1 form.
but on the execute i expected that the foreign key would automatically be generated in the table persons.
Here are my enities
USERS
namespace Geo\CityTroopersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Geo\CityTroopersBundle\Entity\Users
*
* #ORM\Table(name="users")
* #ORM\Entity
*/
class Users
{
//Vanaf hier bijgevoegd
protected $adress;
protected $person;
//Einde zelf toegevoegde properties
/**
* #var integer $userId
*
* #ORM\Column(name="user_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userId;
/**
* #var string $user
*
* #ORM\Column(name="user_password", type="string", length=128, nullable=true)
*/
private $userPassword;
/**
* #var string $userEmail
*
* #ORM\Column(name="user_email", type="string", length=128, nullable=true)
*/
private $userEmail;
/**
* #var string $userImage
*
* #ORM\Column(name="user_image", type="string", length=128, nullable=true)
*/
private $userImage;
/**
* #var string $userQuestion
*
* #ORM\Column(name="user_question", type="string", length=128, nullable=true)
*/
private $userQuestion;
/**
* #var string $userAnswer
*
* #ORM\Column(name="user_answer", type="string", length=128, nullable=true)
*/
private $userAnswer;
/**
* #var \DateTime $userCreateddate
*
* #ORM\Column(name="user_createddate", type="datetime", nullable=true)
*/
/**
* Get userId
*
* #return integer
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set userPassword
*
* #param string $userPassword
* #return Users
*/
public function setUserPassword($userPassword)
{
$this->userPassword = $userPassword;
return $this;
}
/**
* Get userPassword
*
* #return string
*/
public function getUserPassword()
{
return $this->userPassword;
}
/**
* Set userEmail
*
* #param string $userEmail
* #return Users
*/
public function setUserEmail($userEmail)
{
$this->userEmail = $userEmail;
return $this;
}
/**
* Get userEmail
*
* #return string
*/
public function getUserEmail()
{
return $this->userEmail;
}
/**
* Set userImage
*
* #param string $userImage
* #return Users
*/
public function setUserImage($userImage)
{
$this->userImage = $userImage;
return $this;
}
/**
* Get userImage
*
* #return string
*/
public function getUserImage()
{
return $this->userImage;
}
/**
* Set userQuestion
*
* #param string $userQuestion
* #return Users
*/
public function setUserQuestion($userQuestion)
{
$this->userQuestion = $userQuestion;
return $this;
}
/**
* Get userQuestion
*
* #return string
*/
public function getUserQuestion()
{
return $this->userQuestion;
}
/**
* Set userAnswer
*
* #param string $userAnswer
* #return Users
*/
public function setUserAnswer($userAnswer)
{
$this->userAnswer = $userAnswer;
return $this;
}
/**
* Get userAnswer
*
* #return string
*/
public function getUserAnswer()
{
return $this->userAnswer;
}
//Zelf toegevoegde getters & setters
public function getPerson()
{
return $this->person;
}
public function setPerson(Persons $person = null)
{
$this->person = $person;
}
public function getAdress()
{
return $this->adress;
}
public function setAdress(Adresses $adress = null)
{
$this->adress = $adress;
}
}
PERSONS
namespace Geo\CityTroopersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Geo\CityTroopersBundle\Entity\Persons
*
* #ORM\Table(name="persons")
* #ORM\Entity
*/
class Persons
{
/**
* #var integer $personId
*
* #ORM\Column(name="person_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $personId;
/**
* #var integer $adressId
*
* #ORM\Column(name="adress_id", type="integer", nullable=false)
*/
private $adressId;
/**
* #var string $personFamilyname
*
* #ORM\Column(name="person_familyname", type="string", length=64, nullable=false)
*/
private $personFamilyname;
/**
* #var string $personFirstname
*
* #ORM\Column(name="person_firstname", type="string", length=64, nullable=false)
*/
private $personFirstname;
/**
* #var boolean $personGender
*
* #ORM\Column(name="person_gender", type="boolean", nullable=false)
*/
private $personGender;
/**
* #var string $personNationality
*
* #ORM\Column(name="person_nationality", type="string", length=45, nullable=false)
*/
private $personNationality;
/**
* #var Users
*
* #ORM\ManyToOne(targetEntity="Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Get personId
*
* #return integer
*/
public function getPersonId()
{
return $this->personId;
}
/**
* Set adressId
*
* #param integer $adressId
* #return Persons
*/
public function setAdressId($adressId)
{
$this->adressId = $adressId;
return $this;
}
/**
* Get adressId
*
* #return integer
*/
public function getAdressId()
{
return $this->adressId;
}
/**
* Set personFamilyname
*
* #param string $personFamilyname
* #return Persons
*/
public function setPersonFamilyname($personFamilyname)
{
$this->personFamilyname = $personFamilyname;
return $this;
}
/**
* Get personFamilyname
*
* #return string
*/
public function getPersonFamilyname()
{
return $this->personFamilyname;
}
/**
* Set personFirstname
*
* #param string $personFirstname
* #return Persons
*/
public function setPersonFirstname($personFirstname)
{
$this->personFirstname = $personFirstname;
return $this;
}
/**
* Get personFirstname
*
* #return string
*/
public function getPersonFirstname()
{
return $this->personFirstname;
}
/**
* Set personGender
*
* #param boolean $personGender
* #return Persons
*/
public function setPersonGender($personGender)
{
$this->personGender = $personGender;
return $this;
}
/**
* Get personGender
*
* #return boolean
*/
public function getPersonGender()
{
return $this->personGender;
}
/**
* Set personNationality
*
* #param string $personNationality
* #return Persons
*/
public function setPersonNationality($personNationality)
{
$this->personNationality = $personNationality;
return $this;
}
/**
* Get personNationality
*
* #return string
*/
public function getPersonNationality()
{
return $this->personNationality;
}
/**
* Set user
*
* #param Geo\CityTroopersBundle\Entity\Users $user
* #return Persons
*/
public function setUser(\Geo\CityTroopersBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return Geo\CityTroopersBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
CONTROLLER
class UserController extends Controller
{
public function registerAction(Request $request)
{
$user = new Users();
$person = new Persons();
//$adress = new Adresses();
$form = $this->createForm(new UserType(), $user);
if ($request->getMethod() == 'POST'){
$form->bindRequest($request);
if($form->isValid()){
$person = $form->getData();
//$adress = $form->getData();
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->persist($user->getPerson());
//$em->persist($user->getAdress());
$em->flush();
return $this->redirect($this->generateUrl('indexpage'));
}
}
return $this->render('GeoCityTroopersBundle:User:register.html.twig', array(
'form' => $form->createView(),
));
}
}
USERTYPE
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('person', new PersonType());
$builder->add('userEmail');
$builder->add('userPassword', 'password');
$builder->add('userImage');
$builder->add('userQuestion');
$builder->add('userAnswer');
//$builder->add('adress', new AdressType());
}
public function getName()
{
return 'user';
}
}
PERSONTYPE
class PersonType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('personFamilyname');
$builder->add('personFirstname');
$builder->add('personNationality');
$builder->add('personGender');
//$builder->add('adressId');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Geo\CityTroopersBundle\Entity\Persons',
);
}
public function getName()
{
return 'person';
}
}
I have tried to add in the annotations of Users the following:
/**
* Bidirectional (INVERSE SIDE)
* #ORM\OneToOne(targetEntity="Persons",cascade={"persist"})
*
*/
But this didn't worked either.
I have a feeling there is something wrong with the setup of my database, the relation between persons and users is one to one.
update: Deleted unnecessary
Thanks in advance