gpt4 book ai didi

symfony - sonata_type_collection 字段仅适用于现有的父对象

转载 作者:行者123 更新时间:2023-12-02 21:22:17 25 4
gpt4 key购买 nike

在使用 Sonata Admin 包的 Symfony2 应用程序中,我有两个实体:

  • 公司属性
  • CorporateAttributesApi

与教义相关的内容如下:

CorporateAttributes ←一对多→ CorporateAttributesApi

我的 CorporateAttributes Sonata Admin 类包含以下内容:

在 AppBundle/Admin/CorporateAttributesAdmin.php

// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper) {
$formMapper
->add('apis', 'sonata_type_collection',
['required' => false, 'label' => 'API Clients'],
['edit'=>'inline','inline'=>'table']
)
;
}

这会向 CorporateAttributes 表单添加一个“添加新”按钮,我可以在其中添加和编辑与用户正在编辑的 CorporateAttributes 对象相关的 CorporateAttributesApi。

但是,这仅适用于现有的 CorporateAttributes 对象。

如果我尝试添加新的 CorporateAttributes,单击“添加新”按钮会在控制台中出现以下错误:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)
http://localhost/app_dev.php/admin/core/append-form-field-element?code=sonata.admin.corporateattributes&elementId=s55fc29157eeee_apis&uniqid=s55fc29157eeee

我怀疑这与 CorporateAttributesApi 需要它引用的 CorporateAttributes id 有关,但我不确定如何让它发挥良好作用。

这是其他相关代码:

在 AppBundle/Admin/CorporateAttributesApiAdmin.php 中:

// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper) {
$formMapper
->add('corporate_attributes', null, ['required' => true])
->add('group_name', 'choice', [
'choices' => ['a', 'b', 'c'],
'required' => false,
])
;
}

以及带有doctrine2注释的实体:

在 AppBundle/Entity/CorporateAttributes.php 中:

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
* CorporateAttributes
*
*
* @ORM\Entity
* @ORM\Table("drupal_wiredb_corporate_attributes")
*/
class CorporateAttributes
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\OneToMany(targetEntity="CorporateAttributesApi", mappedBy="corporate_attributes", cascade={"persist"}, orphanRemoval=true))
*/
protected $apis;

public function getId() {
return $this->id;
}

/**
* Add apis
*
* @param \AppBundle\Entity\CorporateAttributesApi $apis
* @return CorporateAttributes
*/
public function addApi(\AppBundle\Entity\CorporateAttributesApi $api)
{
$this->apis[] = $api;
$api->setCorporateAttributes($this);

return $this;
}

/**
* Remove apis
*
* @param \AppBundle\Entity\CorporateAttributesApi $apis
*/
public function removeApi(\AppBundle\Entity\CorporateAttributesApi $api)
{
$this->apis->removeElement($api);
$api->setCorporateAttributes(null);
}

/**
* Get apis
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getApis()
{
return $this->apis;
}

/**
* Constructor
*/
public function __construct()
{
$this->apis = new \Doctrine\Common\Collections\ArrayCollection();
}
}

在 AppBundle/Entities/CorporateAttributesApi.php 中:

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
* CorporateAttributesApi
*
*
* @ORM\Entity
* @ORM\Table("drupal_wiredb_corporate_attributes_api")
*/
class CorporateAttributesApi
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="CorporateAttributes", inversedBy="apis")
* @ORM\JoinColumn(name="attribute_id", referencedColumnName="id")
*/
protected $corporate_attributes;

/**
* @ORM\Id
* @ORM\Column(name="group_name", type="string", length=128, options={"default":""})
*/
protected $group_name = '';

public function __toString() {
if (empty($this->corporate_attributes) && empty($this->api_user)) {
return 'New Corporate Attributes - API User Join';
}
else {
return (string)$this->corporate_attributes . ' | ' . (string)$this->api_user . ' | ' . $this->group_name;
}
}

/**
* Set group_name
*
* @param string $groupName
* @return CorporateAttributesApi
*/
public function setGroupName($groupName)
{
$this->group_name = $groupName;

return $this;
}

/**
* Get group_name
*
* @return string
*/
public function getGroupName()
{
return $this->group_name;
}

/**
* Set corporate_attributes
*
* @param \AppBundle\Entity\CorporateAttributes $corporateAttributes
* @return CorporateAttributesApi
*/
public function setCorporateAttributes(\AppBundle\Entity\CorporateAttributes $corporateAttributes)
{
$this->corporate_attributes = $corporateAttributes;

return $this;
}

/**
* Get corporate_attributes
*
* @return \AppBundle\Entity\CorporateAttributes
*/
public function getCorporateAttributes()
{
return $this->corporate_attributes;
}
}

最佳答案

我会尝试通过以下方式修改您的 AppBundle/Admin/CorporateAttributesApiAdmin.php 文件:

// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper) {
$formMapper
->add('corporate_attributes', null, ['required' => true])
->add('group_name', 'choice', [
'choices' => ['a', 'b', 'c'],
'required' => false,
])
;

// If this is sonata_type_collection inside CorporateAttributes form,
// then we don't need 'corporate_attributes' field as it would be the parent entity
if ($this->getRoot() instanceof \AppBundle\Admin\CorporateAttributesAdmin) {
$formMapper->remove('corporate_attributes');
}
}

public function getNewInstance()
{
$object = parent::getNewInstance();

// Here we specify the 'corporate_attributes'
if ($this->getRoot()->getSubject() instanceof \AppBundle\Entity\CorporateAttributes) {
$object->setCorporateAttributes($this->getRoot()->getSubject());
}

return $object;
}

您可能还想尝试修改 AppBundle/Admin/CorporateAttributesAdmin.php 以在表单字段定义中设置 by_reference=false:

// Fields to be shown on create/edit forms
// AppBundle/Admin/CorporateAttributesAdmin.php
protected function configureFormFields(FormMapper $formMapper) {
$formMapper
->add('apis', 'sonata_type_collection',
['required' => false, 'label' => 'API Clients', 'by_reference' => false],
['edit'=>'inline','inline'=>'table']
)
;
}

这里是 by_reference 选项的文档:http://symfony.com/doc/current/reference/forms/types/collection.html#by-reference

关于symfony - sonata_type_collection 字段仅适用于现有的父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32656138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com