【发布时间】:2016-10-14 00:09:51
【问题描述】:
我尝试为产品目录创建树结构。 一个目录可以有多个级别,并且级别可以包含多个产品。
我设法将我的结构保存在数据库中,但是当我想编辑它时,我遇到了这个错误:
Error : Missing value for primary key catalogCode on AppBundle\Entity\CatalogLevel
at OutOfBoundsException ::missingPrimaryKeyValue ('AppBundle\Entity\CatalogLevel', 'catalogCode')
in vendor\doctrine\common\lib\Doctrine\Common\Proxy\AbstractProxyFactory.php at line 125
当我在我的 CatalogController 中执行此操作时:
$form = $this->createForm(CatalogTreeType::class, $catalog);
但是,就在那一行之前,我验证了我是否正确获得了我的关卡,并且看起来就是这样:
// Create an ArrayCollection of the current levels
$originalLevels = new ArrayCollection();
foreach ($catalog->getLevels() as $level) {
var_dump($level->getCatalogCode());
$originalLevels->add($level);
}
// returns
AppBundle\Controller\CatalogController.php:337:string 'TT-FTEST' (length=8)
AppBundle\Controller\CatalogController.php:337:string 'TT-FTEST' (length=8)
CatalogLevel 实体有一个复合键:levelId + catalogCode。
考虑到主键catalogCode不为空,我不明白这个错误...
目录实体
/**
* @ORM\Table(name="catalogue")
* @ORM\Entity(repositoryClass="AppBundle\Entity\CatalogRepository")
* @UniqueEntity(fields="code", message="Catalog code already exists")
*/
class Catalog
{
/**
* @ORM\Column(name="Catalogue_Code", type="string", length=15)
* @ORM\Id
* @Assert\NotBlank()
* @Assert\Length(max=15, maxMessage="The code is too long ({{ limit }} characters max)")
*/
private $code;
/**
* @ORM\OneToMany(targetEntity="CatalogLevel", mappedBy="catalog", cascade={"persist", "remove"})
* @Assert\Valid
*/
private $levels;
/**
* Constructor
*/
public function __construct()
{
$this->levels = new ArrayCollection();
}
/**
* Get levels
*
* @return ArrayCollection
*/
public function getLevels()
{
return $this->levels;
}
/**
* Add level
*
* @param \AppBundle\Entity\CatalogLevel $level
*
* @return Catalog
*/
public function addLevel(\AppBundle\Entity\CatalogLevel $level)
{
$level->setCatalogCode($this->getCode());
$level->setCatalog($this);
if (!$this->getLevels()->contains($level)) {
$this->levels->add($level);
}
return $this;
}
/**
* Remove level
*
* @param \AppBundle\Entity\CatalogLevel $level
*/
public function removeLevel(\AppBundle\Entity\CatalogLevel $level)
{
$this->levels->removeElement($level);
}
}
目录级实体
/**
* @ORM\Table(name="catalogue_niveau")
* @ORM\Entity(repositoryClass="AppBundle\Entity\CatalogLevelRepository")
*/
class CatalogLevel
{
/**
* @ORM\Column(name="Niveau_ID", type="string", length=15)
* @ORM\Id
*/
private $id;
/**
* @ORM\Column(name="Catalogue_Code", type="string", length=15)
* @ORM\Id
*/
private $catalogCode;
/**
* @ORM\ManyToOne(targetEntity="Catalog", inversedBy="levels")
* @ORM\JoinColumn(name="Catalogue_Code", referencedColumnName="Catalogue_Code")
*/
private $catalog;
/**
* Set id
*
* @param string $id
*
* @return CatalogLevel
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Set catalogCode
*
* @param string $catalogCode
*
* @return CatalogLevel
*/
public function setCatalogCode($catalogCode)
{
$this->catalogCode = $catalogCode;
return $this;
}
/**
* Get catalogCode
*
* @return string
*/
public function getCatalogCode()
{
return $this->catalogCode;
}
}
我想提醒您,当我显示预先填写的表单时,editAction 上发生了此错误(它在 addAction 上效果很好)。
感谢您的帮助!
【问题讨论】:
标签: doctrine entity primary-key symfony composite-key