Q:Symfony4 EasyAdmin oneToMany未保存在数据库中
我有两个实体,产品(Produit)和制造商(Fabricant) 当我尝试从Easy Admin更新它们时,如果我在产品方面更改制造商,一切都很好,它工作正常,并且制造商管理员确实显示了正确数量的子产品 然而,如果我用另一种方法——从制造商管理员处选择子产品——它不会将其保存在数据库中。它确实保存名称,但不保存子列表 这里和那里的一些主题表明,我必须确保在addProduct函数中,我还使用$product->setManufacturer($this)。。。我做到了(见下面的代码) 其他人提到,在管理配置中,我应该将by_reference选项设置为false。我也这么做了。但没有成功 另一个建议是确保级联在两个实体之间是正确的,我将其设置为“全部”,直到我能够找出错误,但它仍然不起作用。没有错误消息,没有警告,它甚至保存了其他字段,但不是这个字段。有什么想法吗 产品:Q:Symfony4 EasyAdmin oneToMany未保存在数据库中,symfony,one-to-many,symfony2-easyadmin,Symfony,One To Many,Symfony2 Easyadmin,我有两个实体,产品(Produit)和制造商(Fabricant) 当我尝试从Easy Admin更新它们时,如果我在产品方面更改制造商,一切都很好,它工作正常,并且制造商管理员确实显示了正确数量的子产品 然而,如果我用另一种方法——从制造商管理员处选择子产品——它不会将其保存在数据库中。它确实保存名称,但不保存子列表 这里和那里的一些主题表明,我必须确保在addProduct函数中,我还使用$product->setManufacturer($this)。。。我做到了(见下面的代码) 其他人提
<?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;
}
}
原因是使用具体的$entity调用flush(),所以其他更改不会持久化。您可以等待新版本或新版本
更改控制等级:
easy_admin_bundle:
resource: 'App\Controller\AdminController'
prefix: /admin
type: annotation
扩展管理控制器:
<?php
namespace App\Controller;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;
class AdminController extends BaseAdminController
{
protected function persistEntity($entity)
{
$this->em->persist($entity);
$this->em->flush();
}
protected function updateEntity($entity)
{
$this->em->flush();
}
protected function removeEntity($entity)
{
$this->em->remove($entity);
$this->em->flush();
}
}
我自己也遇到了。以下是我的解决方案,无需覆盖任何控制器操作:
对于任何使用EasyAdminBundlev1.17.08-v1.17.22
的用户,您应该首先升级。显然引入了导致此错误的错误
下一步是确保设置了所属实体中的级联
选项:
@ORM\OneToMany(targetEntity="PageBlock", mappedBy="page", cascade="all", orphanRemoval=true)
还可以在您的表单中设置by_reference
选项:
# in your easy_admin config block...
- property: 'blocks'
type: 'collection'
type_options:
entry_type: ...PageBlockType
by_reference: false
最后但并非最不重要的一点是,我缺少父实体中的add
和remove
方法。还要确保命名正确。因此,对于名为$block
的属性
,请使用以下命令:
public function addBlock(PageBlock $block) : void
{
$block->setPage($this);
$this->blocks->add($block);
}
public function removeBlock(PageBlock $block) : void
{
$block->setPage(null);
$this->blocks->remove($block);
}
这是为我工作
form:
fields:
- { property: 'school' }
- { property: 'companies', type_options: { by_reference: false } }
我有完全相同的问题:(我没有做过的一个测试是在Symfony 3环境中复制相同的情况,这至少有助于确定问题是与SF4相关还是与EasyAdmin相关,我会尝试一下,因为cascade的部分对我帮助很大。不需要移除孤儿。
public function addBlock(PageBlock $block) : void
{
$block->setPage($this);
$this->blocks->add($block);
}
public function removeBlock(PageBlock $block) : void
{
$block->setPage(null);
$this->blocks->remove($block);
}
form:
fields:
- { property: 'school' }
- { property: 'companies', type_options: { by_reference: false } }