Symfony3表单复选框保存错误



我试着在谷歌上查找,但没有找到有这样问题的人。我想我做的一切都像文档指南,但我想我错过了

所以我有一个表格,上面有这样的复选框:

$builder->add(
'productTypes',
EntityType::class,
array(
'label'        => 'Available for products',
'class'        => 'ShopBundle:ProductType',
'choice_label' => 'name',
'multiple'     => true,
'expanded'     => true,
'by_reference' => false,
)
);

当我编辑一切顺利时,我可以编辑现有条目并选中或取消选中此复选框,它会正确保存,但当我尝试添加新对象时,我会收到错误:

PHP致命错误:在中对null调用成员函数add()C: \examplep\htdocs\uniacar sf\src\ShopBundle\Entity\ProductAttribute.php在线188

这是我的控制器操作:

public function editAction(Request $request, $id = null)
{
$this->setMenuTab('cars', 'admin');
$productTypes = new ArrayCollection();
if (!empty($id)) {
$attribute = $this->getRepo(ProductAttribute::class)->find($id);
$this->setTitle('admin.cars.attributes.edit');
foreach ($attribute->getProductTypes() as $value) {
$productTypes->add($value);
}
} else {
$attribute = new ProductAttribute();
$this->setTitle('admin.cars.attributes.new');
}
$form = $this->createForm(ProductAttributeForm::class, $attribute);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$attribute = $form->getData();
foreach ($productTypes as $productType) {
if (false === $attribute->getProductTypes()->contains($productType)) {
$productType->getAttributes()->removeElement($attribute);
$this->db()->persist($productType);
}
}
$this->db()->persist($attribute);
$this->db()->flush();
return $this->redirectToRoute('carAdmin', array('tab' => 'attributes'));
}
$this->setVariables(
array(
'form'      => $form->createView(),
'attribute' => $attribute,
)
);
return $this->response();
}

$this->db()$this->getDoctrine()->getManager()的快捷方式

这是与ProductType:相关的ProductAttribute的定义部分

/**
* Constructor
*/
public function __construct() {
$this->productTypes = new ArrayCollection();
}
/**
* Many Attributes have Many ProductTypes
* @ORMManyToMany(targetEntity="ProductType", mappedBy="attributes", cascade={"persist"})
*/
private $productTypes;
/**
* @param ProductType $productType
*/
public function addProductType(ProductType $productType)
{
$this->productTypes->add($productType);
$productType->addProductAttribute($this);
}
/**
* @param ProductType $productType
*/
public function removeProductType(ProductType $productType)
{
$this->productTypes->removeElement($productType);
}

还有一部分ProductType实体与ProductAttribute:相关

/**
* Constructor
*/
public function __construct() {
$this->attributes = new ArrayCollection();
}
/**
* Many ProductTypes have Many Attributes
* @ORMManyToMany(targetEntity="ProductAttribute", inversedBy="productTypes")
* @ORMJoinTable(name="product_type_to_attribute")
*/
private $attributes;

/**
* @param ProductAttribute $attribute
*/
public function addProductAttribute(ProductAttribute $attribute)
{
if (!$this->attributes->contains($attribute)) {
$this->attributes->add($attribute);
}
}
public function removeProductAttribute(ProductAttribute $attribute)
{
$this->attributes->removeElement($attribute);
}

我试着学习Symfony嵌入表单教程(如何嵌入表单集合)我知道在这种情况下没有嵌入集合(我在这个实体中有另一个字段,那就是表单的嵌入集合,它运行得很好),但据我所知,在这种情况中关系是相同的,是多对多的,所以我必须告诉Symfony如何处理关系,添加和删除对象。我转储了POST中的数据,但它与版本相同——有productType。你知道我为什么会犯这个错误吗?

它在$this->productTypes->add($productType);行的ProductAttribute实体中激发

编辑:我更新了控制器代码,我搞砸了关于从ProductAttribute取消链接ProductType的逻辑。但它对问题没有任何影响,当我试图保存新对象时,仍然是500错误。

第2版:我无法从Symfony获得堆栈跟踪,因为我收到了普通的浏览器500错误(可能是因为它是致命错误,我在apache日志中发现了它)。控制器中产生错误的线路是$form->handleRequest($request);

这不是一个表单集合,但您使用的是特定于集合的方法,这不是一种好的做法,但是,在创建新对象时,您不需要下面的代码。

foreach ($productTypes as $value) {
if (false === $attribute->getProductTypes()->contains($value)) {
$attribute->getProductTypes()->removeElement($value);
}
}

所以,我还没有找到这个问题的解决方案,但我通过修复项目的文件结构(将bundle的Resources从general Resources文件夹移动到bundle的Resources文件夹)以某种方式解决了这个问题。我不知道为什么这解决了这个问题,甚至不知道工作但不正确的文件夹结构和提交表单之间的联系是什么,但现在它起作用了,所以我将把这个问题标记为已回答。

最新更新