我正在尝试使用DataFixtures类中的ManyToOne关系保存数据。我在保存数据时遇到此错误。
请帮助我。
来源:
实体/ DealsCategory.php:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* DealsCategory
*
* @ORM\Table(name="deals_category")
* @ORM\Entity(repositoryClass="AppBundle\Repository\DealsCategoryRepository")
*/
class DealsCategory
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="alias", type="string", length=50, nullable=true)
*/
private $alias;
/**
* @ORM\OneToMany(targetEntity="DealsCategoryLang", mappedBy="category")
*/
protected $categoryLang;
/**
* @return mixed
*/
public function getCategoryLang()
{
return $this->categoryLang;
}
/**
* @param DealsCategoryLang $categoryLang
*/
public function setCategoryLang(DealsCategoryLang $categoryLang = null)
{
$this->categoryLang = $categoryLang;
}
/**
* @param DealsCategoryLang $categoryLang
*/
public function setOneCategoryLang(DealsCategoryLang $categoryLang)
{
$this->categoryLang[] = $categoryLang;
}
/**
* DealsCategory constructor.
*/
public function __construct()
{
$this->categoryLang = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set alias
*
* @param string $alias
*
* @return DealsCategory
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
/**
* Get alias
*
* @return string
*/
public function getAlias()
{
return $this->alias;
}
}
实体/ DealsCategoryLang.php:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* DealsCategoryLang
*
* @ORM\Table(name="deals_category_lang")
* @ORM\Entity(repositoryClass="AppBundle\Repository\DealsCategoryLangRepository")
*/
class DealsCategoryLang
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="catId", type="integer")
*/
private $catId;
/**
* @var string
*
* @ORM\Column(name="lang", type="string", length=10)
*/
private $lang;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=70)
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity="DealsCategory", inversedBy="categoryLang", cascade={"persist"})
* @ORM\JoinColumn(name="catId", referencedColumnName="id")
*
*/
protected $category;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set catId
*
* @param integer $catId
*
* @return DealsCategoryLang
*/
// public function setCatId($catId)
// {
// $this->catId = $catId;
//
// return $this;
// }
/**
* Get catId
*
* @return int
*/
public function getCatId()
{
return $this->catId;
}
/**
* Set lang
*
* @param string $lang
*
* @return DealsCategoryLang
*/
public function setLang($lang)
{
$this->lang = $lang;
return $this;
}
/**
* Get lang
*
* @return string
*/
public function getLang()
{
return $this->lang;
}
/**
* Set title
*
* @param string $title
*
* @return DealsCategoryLang
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
DataFixtures / ORM / LoadCategories.php
<?php
namespace AppBundle\DataFixtures\ORM;
use AppBundle\Entity\DealsCategory;
use AppBundle\Entity\DealsCategoryLang;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LoadCategories implements FixtureInterface, ContainerAwareInterface
{
private $container;
/**
* Load data fixtures with the passed EntityManager
*
* @param ObjectManager $manager
*/
public function load(ObjectManager $manager)
{
$category = new DealsCategory();
$categoryLang = new DealsCategoryLang();
$categoryLang->setLang('en');
$categoryLang->setTitle('Food and Drinks');
$category->setOneCategoryLang($categoryLang);
$manager->persist($category);
$manager->persist($categoryLang);
$manager->flush();
}
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}
我已经尝试过不同的方式,但它仍然无法正常工作。请告诉我,我做错了什么?
UPD: 我的错误是:
[学说\ DBAL \异常\ NotNullConstraintViolationException]
使用params [null,“en”,“Food and Drinks”]执行'INSERT INTO deals_category_lang(catId,lang,title)VALUES(?,?,?)'时发生异常:
SQLSTATE [23000]:完整性约束违规:1048列'catId'不能为空[学说\ DBAL \驱动\ PDOException]
SQLSTATE [23000]:完整性约束违规:1048列'catId'不能为空PDOException]
SQLSTATE [23000]:完整性约束违规:1048列'catId'不能为空
我正在通过命令行运行灯具:
php bin/console doctrine:fixtures:load --append
答案 0 :(得分:0)
好的,我解决了...... 问题发生在我的Fixture类中:
而不是
$category->setOneCategoryLang($categoryLang);
我应该把
$categoryLang->setCategory($category);
,尊重我的实体的层次结构&#39;关系:(