Symfony Doctrine Fixture 意外的空值

Symfony Doctrine Fixture unexpected Null Value

我不明白为什么这段代码发送一个 null 值而不是对另一个对象 entity/record 的实际引用。这是我在生成固定装置时在控制台中收到的消息:

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

学说:夹具:加载

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

这是我的代码:

DataFixtures->ORM->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->ORM->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'],
    ];
}

实体->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();
    }
}

您只是在调用 getter getFkCarMark(),但它从来不是实际的 setter。看样子你需要替换:

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

与:

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

所以它实际上设置了它获取的汽车标记。