Symfony Doctrine Fixture 意外的空值

问题描述 投票:0回答:1

我不明白为什么这段代码发送

null
值而不是对另一个对象实体/记录的实际引用。这是我在生成灯具时在控制台中收到的消息:

有谁知道为什么我在那里得到

null
值,而不是实际值?顺便说一句,不要浪费时间寻找数据循环中的问题。没关系。我已经在没有数据数组和循环的情况下测试了这段代码,问题在单个条目上完全相同。这是一个意想不到的空值。

学说:夹具:负载

[Doctrine\DBAL\Exception\NotNullConstraintViolationException] 一个 执行“INSERT INTO car_model (model, insert_at、last_edited_at、图像、fk_car_mark) 值 (?, ?, ?, ?, ?)' 参数为 ["A3", "2016-07-19 10:27:21", "2016-07-19 10:27:21",
“img”,null]:SQLSTATE [23000]:违反完整性约束:1048 列“fk_car_mark”不能为空

这是我的代码:

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();
    }
}
php symfony fixtures
1个回答
2
投票

你只是调用了 getter

getFkCarMark()
,但它从来不是真正的 setter。从它的外观来看,您需要更换:

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

与:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.