Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 - Able to add data of multiple entities to database but no foreign keys are added
    primarykey
    data
    text
    <p>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. </p> <p>Here are my enities</p> <h3>USERS</h3> <pre class="lang-php prettyprint-override"><code>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-&gt;userId; } /** * Set userPassword * * @param string $userPassword * @return Users */ public function setUserPassword($userPassword) { $this-&gt;userPassword = $userPassword; return $this; } /** * Get userPassword * * @return string */ public function getUserPassword() { return $this-&gt;userPassword; } /** * Set userEmail * * @param string $userEmail * @return Users */ public function setUserEmail($userEmail) { $this-&gt;userEmail = $userEmail; return $this; } /** * Get userEmail * * @return string */ public function getUserEmail() { return $this-&gt;userEmail; } /** * Set userImage * * @param string $userImage * @return Users */ public function setUserImage($userImage) { $this-&gt;userImage = $userImage; return $this; } /** * Get userImage * * @return string */ public function getUserImage() { return $this-&gt;userImage; } /** * Set userQuestion * * @param string $userQuestion * @return Users */ public function setUserQuestion($userQuestion) { $this-&gt;userQuestion = $userQuestion; return $this; } /** * Get userQuestion * * @return string */ public function getUserQuestion() { return $this-&gt;userQuestion; } /** * Set userAnswer * * @param string $userAnswer * @return Users */ public function setUserAnswer($userAnswer) { $this-&gt;userAnswer = $userAnswer; return $this; } /** * Get userAnswer * * @return string */ public function getUserAnswer() { return $this-&gt;userAnswer; } //Zelf toegevoegde getters &amp; setters public function getPerson() { return $this-&gt;person; } public function setPerson(Persons $person = null) { $this-&gt;person = $person; } public function getAdress() { return $this-&gt;adress; } public function setAdress(Adresses $adress = null) { $this-&gt;adress = $adress; } } </code></pre> <h3>PERSONS</h3> <pre class="lang-php prettyprint-override"><code>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-&gt;personId; } /** * Set adressId * * @param integer $adressId * @return Persons */ public function setAdressId($adressId) { $this-&gt;adressId = $adressId; return $this; } /** * Get adressId * * @return integer */ public function getAdressId() { return $this-&gt;adressId; } /** * Set personFamilyname * * @param string $personFamilyname * @return Persons */ public function setPersonFamilyname($personFamilyname) { $this-&gt;personFamilyname = $personFamilyname; return $this; } /** * Get personFamilyname * * @return string */ public function getPersonFamilyname() { return $this-&gt;personFamilyname; } /** * Set personFirstname * * @param string $personFirstname * @return Persons */ public function setPersonFirstname($personFirstname) { $this-&gt;personFirstname = $personFirstname; return $this; } /** * Get personFirstname * * @return string */ public function getPersonFirstname() { return $this-&gt;personFirstname; } /** * Set personGender * * @param boolean $personGender * @return Persons */ public function setPersonGender($personGender) { $this-&gt;personGender = $personGender; return $this; } /** * Get personGender * * @return boolean */ public function getPersonGender() { return $this-&gt;personGender; } /** * Set personNationality * * @param string $personNationality * @return Persons */ public function setPersonNationality($personNationality) { $this-&gt;personNationality = $personNationality; return $this; } /** * Get personNationality * * @return string */ public function getPersonNationality() { return $this-&gt;personNationality; } /** * Set user * * @param Geo\CityTroopersBundle\Entity\Users $user * @return Persons */ public function setUser(\Geo\CityTroopersBundle\Entity\Users $user = null) { $this-&gt;user = $user; return $this; } /** * Get user * * @return Geo\CityTroopersBundle\Entity\Users */ public function getUser() { return $this-&gt;user; } } </code></pre> <h3>CONTROLLER</h3> <pre class="lang-php prettyprint-override"><code>class UserController extends Controller { public function registerAction(Request $request) { $user = new Users(); $person = new Persons(); //$adress = new Adresses(); $form = $this-&gt;createForm(new UserType(), $user); if ($request-&gt;getMethod() == 'POST'){ $form-&gt;bindRequest($request); if($form-&gt;isValid()){ $person = $form-&gt;getData(); //$adress = $form-&gt;getData(); $em = $this-&gt;getDoctrine()-&gt;getEntityManager(); $em-&gt;persist($user); $em-&gt;persist($user-&gt;getPerson()); //$em-&gt;persist($user-&gt;getAdress()); $em-&gt;flush(); return $this-&gt;redirect($this-&gt;generateUrl('indexpage')); } } return $this-&gt;render('GeoCityTroopersBundle:User:register.html.twig', array( 'form' =&gt; $form-&gt;createView(), )); } } </code></pre> <h3>USERTYPE</h3> <pre class="lang-php prettyprint-override"><code>class UserType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('person', new PersonType()); $builder-&gt;add('userEmail'); $builder-&gt;add('userPassword', 'password'); $builder-&gt;add('userImage'); $builder-&gt;add('userQuestion'); $builder-&gt;add('userAnswer'); //$builder-&gt;add('adress', new AdressType()); } public function getName() { return 'user'; } } </code></pre> <h3>PERSONTYPE</h3> <pre class="lang-php prettyprint-override"><code>class PersonType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('personFamilyname'); $builder-&gt;add('personFirstname'); $builder-&gt;add('personNationality'); $builder-&gt;add('personGender'); //$builder-&gt;add('adressId'); } public function getDefaultOptions(array $options) { return array( 'data_class' =&gt; 'Geo\CityTroopersBundle\Entity\Persons', ); } public function getName() { return 'person'; } } </code></pre> <p>I have tried to add in the annotations of Users the following:</p> <pre><code>/** * Bidirectional (INVERSE SIDE) * @ORM\OneToOne(targetEntity="Persons",cascade={"persist"}) * */ </code></pre> <p>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.</p> <p>update: Deleted unnecessary</p> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload