Symfony Doctrine Fixture unexpected Null Value
<p>I can't figure out why this code sends a <code>null</code> value instead of an actual reference to another object entity/record.. This is the message I get in my console while generating my fixtures:</p>

<p>Does anyone know why I get the <code>null</code> value there, instead the actual value ? By the way, don't waste your time looking for an issue in the data looping. It's fine. I have tested this code without data arrays and loop the issue was exactly the same on single entry. That is an unexpected null value.</p>

<p><strong>doctrine:fixtures:load</strong></p>

<blockquote>
  <p>[Doctrine\DBAL\Exception\NotNullConstraintViolationException]   An
  exception occurred while executing 'INSERT INTO car_model (model,
  inserted_at, last_edited_at, image, <strong>fk_car_mark</strong>) VALUES (?, ?, ?, ?,
  ?)' with params ["A3", "2016-07-19 10:27:21", "2016-07-19 10:27:21",<br>
  "img", <strong>null</strong>]:   SQLSTATE[23000]: Integrity constraint violation: 1048
  Column 'fk_car_mark' <strong>cannot be null</strong></p>
</blockquote>

<p>This is my code:</p>

<p><strong>DataFixtures->ORM->LoadCarMarkData.php</strong></p>

<pre><code>&lt;?php

namespace AdminBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use AdminBundle\Entity\CarModel;

class LoadCarModelData extends AbstractFixture implements OrderedFixtureInterface
{
    public function load(ObjectManager $manager)
    {
        foreach($this-&gt;data as $mark =&gt; $modelArray){
            // New Entry ----
            foreach($modelArray as $model){
            $carModel = new CarModel();
            $carModel-&gt;setModel($model);
            $carModel-&gt;setImage('img');
            $carModel-&gt;getFkCarMark($this-&gt;getReference($mark));

            $manager-&gt;persist($carModel);
            $manager-&gt;flush(); 
            }
        }
    }

    public function getOrder()
    {
        // the order in which fixtures will be loaded
        // the lower the number, the sooner that this fixture is loaded
        return 4;
    }

    private $data = [
        'AUDI' =&gt; [
            'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8',
            'Q2', 'Q3', 'Q5', 'Q7',
            'TT', 'R8'
        ],
        'SKODA' =&gt; [
            'Yeti', 'Roomster', 'Superb', 'Rapid', 'Fabia', 'Mission', 'Joyster', 
        ],
    ];
}
</code></pre>

<p><strong>DataFixtures->ORM->LoadCarModelData.php</strong></p>

<pre><code>&lt;?php

namespace AdminBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

use AdminBundle\Entity\CarMark;

class LoadCarMarkData extends AbstractFixture implements OrderedFixtureInterface
{

    public function load(ObjectManager $manager)
    {
        foreach($this-&gt;data as $entry){
            $carMark = new CarMark();
            $carMark-&gt;setMark(str_replace([' ','/','\\'], ['_'], $entry['mark']));   

            $manager-&gt;persist($carMark);
            $manager-&gt;flush();

            $this-&gt;addReference(str_replace([' ','/','\\'], ['_'], $entry['mark']), $carMark);
        }


    }

    public function getOrder() {
        return 3;
    }

    private $data = [
        ['mark'=&gt;'ACURA'],
        ['mark'=&gt;'ALFA ROMEO'],
        ['mark'=&gt;'AMC'],
        ['mark'=&gt;'ARIEL'],
        ['mark'=&gt;'ASTON MARTIN'],
        ['mark'=&gt;'AUDI'],
        ['mark'=&gt;'AUSTIN HEALEY'],
        ['mark'=&gt;'BENTLEY'],
        ['mark'=&gt;'BMW'],
        ['mark'=&gt;'BUGATTI'],
        ['mark'=&gt;'BUICK'],
        ['mark'=&gt;'CADILLAC'],
        ['mark'=&gt;'CALLAWAY'],
        ['mark'=&gt;'CATERHAM'],
        ['mark'=&gt;'CHEVROLET'],
        ['mark'=&gt;'CHRYSLER'],
        ['mark'=&gt;'CITROEN'],
        ['mark'=&gt;'DAEWOO'],
        ['mark'=&gt;'DAIHATSU'],
        ['mark'=&gt;'DATSUN'],
        ['mark'=&gt;'DE TOMASO'],
        ['mark'=&gt;'DODGE'],
        ['mark'=&gt;'EAGLE'],
        ['mark'=&gt;'FERRARI'],
        ['mark'=&gt;'FIAT'],
        ['mark'=&gt;'FISKER'],
        ['mark'=&gt;'FORD'],
        ['mark'=&gt;'GENESIS'],
        ['mark'=&gt;'GEO'],
        ['mark'=&gt;'GMC'],
        ['mark'=&gt;'HOLDEN'],
        ['mark'=&gt;'HONDA'],
        ['mark'=&gt;'HUMMER'],
        ['mark'=&gt;'HYUNDAI'],
        ['mark'=&gt;'INFINITI'],
        ['mark'=&gt;'ISUZU'],
        ['mark'=&gt;'JAGUAR'],
        ['mark'=&gt;'JEEP'],
        ['mark'=&gt;'KIA'],
        ['mark'=&gt;'KOENIGSEGG'],
        ['mark'=&gt;'LAMBORGHINI'],
        ['mark'=&gt;'LANCIA'],
        ['mark'=&gt;'LAND ROVER'],
        ['mark'=&gt;'LEXUS'],
        ['mark'=&gt;'LINCOLN'],
        ['mark'=&gt;'LOTUS'],
        ['mark'=&gt;'MASERATI'],
        ['mark'=&gt;'MAYBACH'],
        ['mark'=&gt;'MAZDA'],
        ['mark'=&gt;'MCLAREN'],
        ['mark'=&gt;'MERCEDES'],
        ['mark'=&gt;'MERCURY'],
        ['mark'=&gt;'MG'],
        ['mark'=&gt;'MINI'],
        ['mark'=&gt;'MITSUBISHI'],
        ['mark'=&gt;'MORGAN'],
        ['mark'=&gt;'MOSLER / ROSSION'],
        ['mark'=&gt;'NISSAN'],
        ['mark'=&gt;'NOBLE'],
        ['mark'=&gt;'OLDSMOBILE'],
        ['mark'=&gt;'OPEL'],
        ['mark'=&gt;'PAGANI'],
        ['mark'=&gt;'PEUGEOT'],
        ['mark'=&gt;'PLYMOUTH'],
        ['mark'=&gt;'PONTIAC'],
        ['mark'=&gt;'PORSCHE'],
        ['mark'=&gt;'PROTON'],
        ['mark'=&gt;'RAM'],
        ['mark'=&gt;'RENAULT'],
        ['mark'=&gt;'ROLLS-ROYCE'],
        ['mark'=&gt;'SAAB'],
        ['mark'=&gt;'SALEEN'],
        ['mark'=&gt;'SATURN'],
        ['mark'=&gt;'SCION'],
        ['mark'=&gt;'SEAT'],
        ['mark'=&gt;'SHELBY'],
        ['mark'=&gt;'SKODA'],
        ['mark'=&gt;'SMART'],
        ['mark'=&gt;'SSANGYONG'],
        ['mark'=&gt;'SUBARU'],
        ['mark'=&gt;'SUZUKI'],
        ['mark'=&gt;'TESLA'],
        ['mark'=&gt;'TOYOTA'],
        ['mark'=&gt;'TRIUMPH'],
        ['mark'=&gt;'VAUXHALL'],
        ['mark'=&gt;'VW'],
        ['mark'=&gt;'VOLVO'],
        ['mark'=&gt;'WESTFIELD'],
    ];
}
</code></pre>

<p><strong>Entity->CarModel.php</strong></p>

<pre><code>&lt;?php

namespace AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AdminBundle\Entity\CarMark;

/**
 * CarModel
 *
 * @ORM\Table(name="car_model")
 * @ORM\Entity(repositoryClass="AdminBundle\Repository\CarModelRepository")
 * @ORM\HasLifecycleCallbacks
 */
class CarModel
{
    /**
     * @var string
     *
     * @ORM\Column(name="model", type="string", length=255)
     * @ORM\Id
     * 
     */
    private $model;

    /**
     * @var \DateTime
     * @ORM\Column(name="inserted_at", type="datetime", nullable=true)  
     */
    private $insertedAt;

    /**
     * @var \DateTime
     * @ORM\Column(name="last_edited_at", type="datetime", nullable=true)  
     */
    private $lastEditedAt;    

    /**
     * @var string
     *
     * @ORM\Column(name="image", type="string", length=255)
     */
    private $image;

    /**
     * @ORM\ManyToOne(targetEntity="CarMark", inversedBy="mark")
     * @ORM\JoinColumn(name="fk_car_mark", referencedColumnName="mark", nullable=false)
     */
    private $fkCarMark;

    /**
     * Set insertedAt
     *
     * @param \DateTime $insertedAt
     *
     * @return CarMark
     */
    public function setInsertedAt(\DateTime $insertedAt)
    {
        $this-&gt;insertedAt = $insertedAt;

        return $this;
    }

    /**
     * Get insertedAt
     *
     * @return \DateTime
     */
    public function getInsertedAt()
    {
        return $this-&gt;insertedAt;
    }

    /**
     * Set lastEditedAt
     *
     * @param \DateTime $lastEditedAt
     *
     * @return CarMark
     */
    public function setLastEditedAt(\DateTime $lastEditedAt)
    {
        $this-&gt;lastEditedAt = $lastEditedAt;

        return $this;
    }

    /**
     * Get lastEditedAt
     *
     * @return \DateTime
     */
    public function getLastEditedAt()
    {
        return $this-&gt;lastEditedAt;
    }

    /**
    *
    * @ORM\PrePersist
    * @ORM\PreUpdate
    */
   public function updatedTimestamps()
   {
       $this-&gt;setLastEditedAt(new \DateTime('now'));

       if ($this-&gt;getInsertedAt() == null) {
           $this-&gt;setInsertedAt(new \DateTime('now'));
       }
   }

    /**
     * Set model
     *
     * @param string $model
     *
     * @return CarModel
     */
    public function setModel($model)
    {
        $this-&gt;model = $model;

        return $this;
    }

    /**
     * Get model
     *
     * @return string
     */
    public function getModel()
    {
        return $this-&gt;model;
    }

    /**
     * Set image
     *
     * @param string $image
     *
     * @return CarModel
     */
    public function setImage($image)
    {
        $this-&gt;image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string
     */
    public function getImage()
    {
        return $this-&gt;image;
    }

    /**
     * Set fkCarMark
     *
     * @param \AdminBundle\Entity\CarMark $fkCarMark
     *
     * @return CarModel
     */
    public function setFkCarMark(\AdminBundle\Entity\CarMark $fkCarMark)
    {
        $this-&gt;fkCarMark = $fkCarMark;

        return $this;
    }

    /**
     * Get fkCarMark
     *
     * @return \AdminBundle\Entity\CarMark
     */
    public function getFkCarMark()
    {
        return $this-&gt;fkCarMark;
    }

    public function __toString() {
        return $this-&gt;getModel();
    }
}
</code></pre>