2015-09-18 41 views
3

在使用索納塔管理包一個Symfony2的應用程序的工作,我有兩個實體:sonata_type_collection場只與現有的父對象

  • CorporateAttributes
  • CorporateAttributesApi

教義相關像這樣:

個CorporateAttributes←一個一對多→CorporateAttributesApi

爲CorporateAttributes

我索納塔管理類包含以下內容:

中的appbundle /管理/ 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 /管理/ 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 /實體/ 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(); 
    } 
} 

in AppBundle/Entities/Co rporateAttributesApi.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; 
    } 
} 
+0

由於500錯誤,瀏覽器開發人員面板的「網絡」選項卡是否顯示任何HTML響應,如引發的異常消息? – Vasily802

回答

0

我會嘗試修改您的appbundle /管理/ 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 /管理/ CorporateAttributesAdmin.php設置by_reference =假在表單字段的定義:

// 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