製品カタログのツリー構造を作成しようとしています。 カタログには複数のレベルがあり、レベルには複数の製品を含めることができます。Symfony3エンティティの編集:エラープライマリキーの値がありません
私は、データベースに私の構造を保存するために管理が、私はそれを編集したいとき、私はこのエラーを持っている:ちょうど、
$form = $this->createForm(CatalogTreeType::class, $catalog);
しかし:
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でこれを行うとき、その前にレベルを正しく取得しているかどうかを確認してみてください。
// 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
エンティティには、複合キーkey: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);
}
}
CatalogLevelエンティティ
/**
* @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;
}
}
ではありません
私は好きですeは、このエラーがeditActionで発生したことを示しています(addActionではうまく動作します)。
ありがとうございました!
おかげで、私はjQueryを使って私のツリーを作成し、私は後に提出したフォームデータとIDを修正するので、私はCatalogLevelのための自動インクリメントIDを使用しないでください。たとえば、レベルIDはitem-1、item-2、item-xxのようなものです。 データベースの保存がうまく機能します。 "catalogue_niveau"テーブルにすべてのレベルデータがあります。さらに、AbstractProxyFactoryクラスで、エンティティCatalogLevelに直接的にではなくエラーが発生しました。 – Felurian
だから、何が原因か分かりませんが、簡単な方法があります。 [doctrine tree extension](https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#including-extension)を使うことができます。 1つのテーブルにすべての魔法。 – ciurciurek
私はそれを知らない、ありがとう!それが私の構造に対応しているかどうかがわかります。 – Felurian