无法获取相关实体字段以保存在 Symfony 管理表单中
Cannot get related entity field to save in Symfony Admin Form
我正在研究 Symfony,试图了解它是如何组合在一起的,我正在研究管理部分。
现在我正在为一个节目实体整理一个管理表单,它将引用一个部分实体(所以这个节目属于那个部分,等等)。除了相关的实体选择字段外,表单中的每个其他字段都保存。
这是 ShowAdmin class
<?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();
}
}
任何帮助将不胜感激,我知道这可能是我只是没有看到的超级简单的东西。
谢谢。
- (可选)将
ShowEntity::$section
重命名为 ShowEntity::$sections
以突出显示 sections
是一个集合而不是单个实体。
将ShowEntity
__construct
方法体设置为:
$this->sections = new \Doctrine\Common\Collections\ArrayCollection();
在ShowAdmin::configureFormFields
重命名
->add('section_id', EntityType::class, array(
进入
->add('section', EntityType::class, array(
您应该使用对关系的直接引用而不是 id
。
- 删除
SectionEntity::__construct
方法,没有意义。
- 从
SectionEntity
中删除 protected $section_id;
。
- 将
public function setSectionId($sectionId)
改为public function setSection(Section $section)
。
- 也许您还需要将
section_title
重命名为 sectionTitle
或简单地 title
,不确定。
我正在研究 Symfony,试图了解它是如何组合在一起的,我正在研究管理部分。
现在我正在为一个节目实体整理一个管理表单,它将引用一个部分实体(所以这个节目属于那个部分,等等)。除了相关的实体选择字段外,表单中的每个其他字段都保存。
这是 ShowAdmin class
<?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();
}
}
任何帮助将不胜感激,我知道这可能是我只是没有看到的超级简单的东西。
谢谢。
- (可选)将
ShowEntity::$section
重命名为ShowEntity::$sections
以突出显示sections
是一个集合而不是单个实体。 将
ShowEntity
__construct
方法体设置为:$this->sections = new \Doctrine\Common\Collections\ArrayCollection();
在
ShowAdmin::configureFormFields
重命名->add('section_id', EntityType::class, array(
进入
->add('section', EntityType::class, array(
您应该使用对关系的直接引用而不是
id
。- 删除
SectionEntity::__construct
方法,没有意义。 - 从
SectionEntity
中删除protected $section_id;
。 - 将
public function setSectionId($sectionId)
改为public function setSection(Section $section)
。 - 也许您还需要将
section_title
重命名为sectionTitle
或简单地title
,不确定。