【发布时间】:2018-06-26 12:40:43
【问题描述】:
我有 2 个实体,产品(产品)和制造商(制造商)。
当我尝试从 Easy Admin 更新它们时,如果我在产品端更改制造商,一切都很好,它可以正常工作,并且制造商管理员确实显示了适量的子产品。
但是,如果我反过来 - 从制造商管理员中选择子产品 - 它不会将其保存在数据库中。它确实保存名称,但不保存子列表。
这里和那里的一些主题表明我必须确保在 addProduct 函数中,我还使用 $product->setManufacturer($this); 对产品进行操作...我做了(见下面的代码)。
其他人提到,在管理配置中,我应该将 by_reference 选项设置为 false。我也这样做了。然而没有成功。
另一个建议是确保 2 个实体之间的级联是正确的,我已将其设为“全部”,直到找出问题所在,但它仍然不起作用。没有错误消息,没有警告,它甚至保存了其他字段,但不是这个。有什么想法吗?
产品:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ProduitRepository")
*/
class Produit
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $nom;
/**
* @ORM\ManyToOne(targetEntity="Fabricant", inversedBy="produits", cascade="all")
*/
private $fabricant;
public function __toString()
{
return ($this->nom != null) ? $this->nom : '';
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getNom()
{
return $this->nom;
}
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
public function getFabricant()
{
return $this->fabricant;
}
public function setFabricant($fabricant)
{
$this->fabricant = $fabricant;
return $this;
}
}
制造商:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="App\Repository\FabricantRepository")
*/
class Fabricant
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $nom;
/**
* @ORM\OneToMany(targetEntity="Produit", mappedBy="fabricant", cascade="all")
*/
private $produits;
public function __toString()
{
return ($this->nom != null) ? $this->nom : '';
}
public function __construct()
{
$this->produits = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getNom()
{
return $this->nom;
}
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
public function getProduits()
{
return $this->produits;
}
public function addProduit(Produit $produit)
{
if ($this->produits->contains($produit)) {
return;
}
$this->produits[] = $produit;
$produit->setFabricant($this);
return $this;
}
public function removeProduit($produit)
{
$this->produits->removeElement($produit);
$produit->setFabricant(null);
}
}
轻松管理 yaml 配置:
easy_admin:
entities:
Produit:
class: App\Entity\Produit
list:
fields:
- id
- nom
- fabricant
new:
fields:
- nom
- { property: 'fabricant', type_options: { 'by_reference': false } }
Fabricant:
class: App\Entity\Fabricant
list:
fields:
- id
- nom
- produits
new:
fields:
- nom
- { property: 'produits', type_options: { by_reference: false } }
edit:
fields:
- nom
- { property: 'produits', type_options: { multiple: true, by_reference: false } }
【问题讨论】:
-
我有完全相同的问题:(
-
我没有做过的一个测试是在 Symfony 3 环境中复制相同的情况,这至少有助于确定问题是与 SF4 相关还是与 EasyAdmin 相关,我会尽快尝试
标签: symfony one-to-many symfony2-easyadmin