Symfony Doctrine Fixture意外的空值

时间:2016-07-19 08:49:55

标签: symfony fixture

我无法弄清楚为什么这段代码发送null值而不是实际引用另一个对象实体/记录。这是我在生成我的灯具时在控制台中得到的消息:< / p>

有谁知道我为什么会得到null值,而不是实际值?顺便说一句,不要浪费时间在数据循环中寻找问题。没关系。我已经测试了没有数据数组的代码,循环问题在单个条目上完全相同。这是一个意外的空值。

教义:夹具:负载

  

[Doctrine \ DBAL \ Exception \ NotNullConstraintViolationException] An   执行&#39; INSERT INTO car_model(模型,   inserted_at,last_edited_at,image, fk_car_mark )VALUES(?,?,?,?,   ?)&#39;与params [&#34; A3&#34;,&#34; 2016-07-19 10:27:21&#34;,&#34; 2016-07-19 10:27:21&#34;,
  &#34; img&#34;, null ]:SQLSTATE [23000]:完整性约束违规:1048   专栏&#39; fk_car_mark&#39; 不能为空

这是我的代码:

DataFixtures-&GT; ORM-&GT; LoadCarMarkData.php

<?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->data as $mark => $modelArray){
            // New Entry ----
            foreach($modelArray as $model){
            $carModel = new CarModel();
            $carModel->setModel($model);
            $carModel->setImage('img');
            $carModel->getFkCarMark($this->getReference($mark));

            $manager->persist($carModel);
            $manager->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' => [
            'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8',
            'Q2', 'Q3', 'Q5', 'Q7',
            'TT', 'R8'
        ],
        'SKODA' => [
            'Yeti', 'Roomster', 'Superb', 'Rapid', 'Fabia', 'Mission', 'Joyster', 
        ],
    ];
}

DataFixtures-&GT; ORM-&GT; LoadCarModelData.php

<?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->data as $entry){
            $carMark = new CarMark();
            $carMark->setMark(str_replace([' ','/','\\'], ['_'], $entry['mark']));   

            $manager->persist($carMark);
            $manager->flush();

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


    }

    public function getOrder() {
        return 3;
    }

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

实体 - &GT; CarModel.php

<?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->insertedAt = $insertedAt;

        return $this;
    }

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

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

        return $this;
    }

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

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

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    public function __toString() {
        return $this->getModel();
    }
}

1 个答案:

答案 0 :(得分:1)

你只是在调用getter getFkCarMark(),但从来不是它的真正的二传手。看起来你需要替换:

$carModel->getFkCarMark($this->getReference($mark));

使用:

$carModel->setFkCarMark($this->getReference($mark));

所以它实际上设定了它所取得的汽车标记。