このコードは、別のオブジェクトの実体/レコードに実際の参照の代わりにnull
値を送信し、なぜ私が理解することはできません。これは私の備品を発生させながら、私は私のコンソールで取得メッセージです。symfonyの教義フィクス予期しないNULL値
私はなぜnull
という値を代わりに実際の値にするのか知っていますか?ところで、データのループで問題を探すのに時間を費やさないでください。大丈夫だよ。私はこのコードをデータ配列とループなしでテストしましたが、問題は単一エントリで全く同じでした。これは予期しないヌル値です。
教義:器具:負荷
car_model。INSERT INTO」実行中 例外が発生[NotNullConstraintViolationException \教義\ DBAL \例外](モデル、 inserted_at、last_edited_at、画像、fk_car_mark) params ["A3"、 "2016-07-19 10:27:21"、 "2016-07-19 10:27:21"、
"とVALUES(?、?、?、?、 ? img "、null]:SQLSTATE [23000]:整合性制約違反:1048 列 'fk_car_mark' は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'],
];
}
ENTITY-> 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();
}
}
あなたは 'getFkCarMark'を呼び出しますが、実際のセッターは決して呼び出しません。したがって、nullのままです。 – Oldskool
ありがとうございます。だから;) – DevWL