2016-12-21 50 views
0

我正在通過Symfony嘗試學習如何將它們放在一起,我正在處理管理部分。無法獲取相關實體字段以保存在Symfony Admin表格

現在,我正在爲顯示實體組織一個管理員表單,這個管理員表單將引用一個節實體(所以這個節目屬於該節等)。除了相關的實體選擇字段之外,表單中的其他每個字段都保存爲EXCEPT。

這是ShowAdmin類

<?php 

namespace AppBundle\Admin; 

use Sonata\AdminBundle\Admin\AbstractAdmin; 
use Sonata\AdminBundle\Datagrid\ListMapper; 
use Sonata\AdminBundle\Datagrid\DatagridMapper; 
use Sonata\AdminBundle\Form\FormMapper; 
use Nelmio\ApiDocBundle\Tests\Fixtures\Form\EntityType; 

class ShowAdmin extends AbstractAdmin { 
    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper->add('title', 'text') 
        ->add('shortname', 'text') 
        ->add('section_id', EntityType::class, array(
         'class' => 'AppBundle:SectionEntity', 
         'choice_label' => 'section_title', 
        )) 
        ->add('logo', 'text') 
        ->add('description', 'textarea') 
        ->add('status', 'integer'); 
    } 

    protected function configureDatagridFilters(DatagridMapper $datagridMapper) 
    { 
     $datagridMapper->add('title'); 
     $datagridMapper->add('shortname'); 
    } 

    protected function configureListFields(ListMapper $listMapper) 
    { 
     $listMapper->addIdentifier('title'); 
     $listMapper->add('shortname', 'text'); 
    } 
} 

這是ShowEntity

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="shows") 
*/ 
class ShowEntity { 

    function __construct() { 
     $this->children = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 


    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    private $title; 

    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    private $shortname; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\SectionEntity") 
    */ 
    private $section; 

    /** 
    * @ORM\Column(type="string", length=255) 
    */ 
    private $logo; 

    /** 
    * @ORM\Column(type="text") 
    */ 
    private $description; 

    /** 
    * @ORM\Column(type="integer") 
    */ 
    private $status; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set title 
    * 
    * @param string $title 
    * 
    * @return ShowEntity 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 

     return $this; 
    } 

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

    /** 
    * Set sectionId 
    * 
    * @param integer $sectionId 
    * 
    * @return ShowEntity 
    */ 
    public function setSectionId($sectionId) 
    { 
     $this->section_id = $sectionId; 

     return $this; 
    } 

    /** 
    * Get sectionId 
    * 
    * @return integer 
    */ 
    public function getSectionId() 
    { 
     return $this->section_id; 
    } 

    /** 
    * Set logo 
    * 
    * @param string $logo 
    * 
    * @return ShowEntity 
    */ 
    public function setLogo($logo) 
    { 
     $this->logo = $logo; 

     return $this; 
    } 

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

    /** 
    * Set description 
    * 
    * @param string $description 
    * 
    * @return ShowEntity 
    */ 
    public function setDescription($description) 
    { 
     $this->description = $description; 

     return $this; 
    } 

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

    /** 
    * Set status 
    * 
    * @param integer $status 
    * 
    * @return ShowEntity 
    */ 
    public function setStatus($status) 
    { 
     $this->status = $status; 

     return $this; 
    } 

    /** 
    * Get status 
    * 
    * @return integer 
    */ 
    public function getStatus() 
    { 
     return $this->status; 
    } 

    /** 
    * Set shortname 
    * 
    * @param string $shortname 
    * 
    * @return ShowEntity 
    */ 
    public function setShortname($shortname) 
    { 
     $this->shortname = $shortname; 

     return $this; 
    } 

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

這是SectionEntity

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* SectionEntity 
* 
* @ORM\Table(name="section_entity") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\SectionEntityRepository") 
*/ 
class SectionEntity 
{ 

    protected $section_id; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="text") 
    */ 
    private $section_title; 

    /** 
    * Get id 
    * 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set sectionTitle 
    * 
    * @param string $sectionTitle 
    * 
    * @return SectionEntity 
    */ 
    public function setSectionTitle($sectionTitle) 
    { 
     $this->section_title = $sectionTitle; 

     return $this; 
    } 

    /** 
    * Get sectionTitle 
    * 
    * @return string 
    */ 
    public function getSectionTitle() 
    { 
     return $this->section_title; 
    } 

    /** 
    * Get string 
    */ 
    public function __toString() { 
     return $this->section_title; 
    } 

    function __construct() { 
     $this->section_id = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

} 

任何幫助將不勝感激,我知道這是可能超級簡單的東西,我只是沒有看到。

謝謝。

回答

1
  1. (可選)重命名ShowEntity::$sectionShowEntity::$sections突出sections是一個集合而不是一個單一的實體。
  2. 設置ShowEntity__construct方法體:

    $this->sections = new \Doctrine\Common\Collections\ArrayCollection(); 
    
  3. ShowAdmin::configureFormFields重命名

    ->add('section_id', EntityType::class, array(
    

    ->add('section', EntityType::class, array(
    

    您應該使用直接引用的關係,而不是id

  4. 刪除SectionEntity::__construct方法,它沒有任何意義。
  5. SectionEntity刪除protected $section_id;
  6. public function setSectionId($sectionId)更改爲public function setSection(Section $section)
  7. 也許您還需要將section_title重命名爲sectionTitle或者簡單地title,對此不確定。
+0

謝謝抱歉,遲到的反應,聖誕節和所有。一旦時間允許,請嘗試一下 – purserj

+1

謝謝,這似乎已經完成了。 – purserj

相關問題