2016-12-21 1 views
0

Я работаю через Symfony, пытаясь узнать, как все это сочетается, и я работаю над секцией администратора.Не удается получить связанное сущ. Поле для сохранения в Symfony Admin Form

В настоящее время я собираю административную форму для представления объекта, который будет ссылаться на объект раздела (так что это шоу принадлежит этому разделу и т. Д.). Каждое другое поле в форме сохраняет 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::$section в ShowEntity::$sections, чтобы выделить sections коллекция, но не единое целое.
  2. Set 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. Удалить protected $section_id; от SectionEntity.
  6. public function setSectionId($sectionId)public function setSection(Section $section).
  7. Возможно, вам также необходимо переименовать section_title в sectionTitle или просто title, не уверен в этом.
+0

Прошу прощения за задержанную реакцию, Рождество и все. Попробуем попробовать один раз, когда позволяет – purserj

+1

Спасибо, что, похоже, сделали трюк. – purserj

Смежные вопросы