Invis00
Invis00

Reputation: 71

Doctrine generates NULLinstead of setted value

When I try to store values to DB with symfony via doctrine, doctrine produces a NULL for a field although it is set and I have no idea why.

Entity: https://github.com/Invis00/pub/blob/master/OldbCompetitionstage.php

Test Controller:

public function compTest(){
    $em = $this->getDoctrine()->getManager();

    $object = new OldbCompetitionstage();
    $object->setName("name");
    $object->setNr(2);
    $object->setOldbCompetitionid(2);

    // Echo tells 2
    echo $object->getOldbCompetitionid();

    $em->persist($object);
    $em->flush();

    return $this->render('base.html.twig', array( "current" => "pubmin")
    );
}

Table:

compstageID int(11)
name    varchar(100)
nr  int(11)
oldb_competitionID  int(11)
startDate   datetime
ipub_compstageID    int(11)

Symfony Profiler tells:

INSERT INTO oldb_competitionstage (name, nr, oldb_competitionID, startDate, ipub_compstageID) VALUES (?, ?, ?, ?, ?)
  Parameters: { 1: name, 2: 2, 3: null, 4: null, 5: null }

But why is oldb_competitionid NULL instead of 2? echo tells me it is 2.

It seems that the mapping information for competition is in some kind the reason for this behaviour but there are no errors told and I dont see the problem with it.

Upvotes: 0

Views: 41

Answers (1)

Veve
Veve

Reputation: 6748

Your problem come from $oldbCompetitionid in your class. It doesn't have any reason to exist, and because you assigned the same column name than the one used in your ManyToOne ORM column definition, it causes the NULL.

As chalasr commented,

you can't neither define a joinColumn as a member of your class nor set its value as a simple column, with or without ORM

Remove it and check the relation definitions between your classes.

The problematic part of the code:

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @ORM\Entity
 * @ORM\Table(name="oldb_competitionstage")
 */
class OldbCompetitionstage
{
    /**
     * @var integer
     *
     * @ORM\Column(name="compstageID", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $compstageid;
    /**
     * @var integer
     *
     * @ORM\Column(name="oldb_competitionID", type="integer", nullable=true)
     */
    private $oldbCompetitionid;
    /**
     * Mappings
     */
    /**
     * @ORM\ManyToOne(targetEntity="OldbCompetition", inversedBy="compstages")
     * @ORM\JoinColumn(name="oldb_competitionID", referencedColumnName="competitionID")
     */
    private $oldbCompetition;

You should have something like this in your OldbCompetition class:

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @ORM\Entity
 * @ORM\Table(name="oldb_competitionstage")
 */
class OldbCompetition
{
    /**
     * @var integer
     *
     * @ORM\Column(name="competitionID", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $compid;
     /**
     * @ORM\ManyToOne(targetEntity="OldbCompetition", mappedBy="oldbCompetition")
     */
    private $compstages;

Upvotes: 1

Related Questions