将 sonata_type_collection 用于一对多一关系的问题
Posted
技术标签:
【中文标题】将 sonata_type_collection 用于一对多一关系的问题【英文标题】:Problems using a sonata_type_collection for a One-Many-One relation 【发布时间】:2012-09-23 08:51:07 【问题描述】:我有一个相当常见的用例,我正在尝试实现它,但在 Symfony Sonata Admin Bundle (ORM) 上遇到了一些问题。我的模型在 Facility 和 Sport 之间建立了关系,该关系基于三个实体类:Sport、Facility 和 SportsFacility。我遵循示例http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#advanced-usage-one-to-many 并在以下类中定义(仅相关部分)。
class Sport
/**
* Bidirectional - Many facilies are related to one sport
*
* @ORM\OneToMany(targetEntity="SportsFacility", mappedBy="sport")
*/
protected $facilities;
public function getFacilities()
return $this->facilities;
public function setFacilities($facilities)
$this->facilities = $facilities;
return $this;
class Facility
/**
* Bidirectional - Many sports are related to one facility
*
* @ORM\OneToMany(targetEntity="SportsFacility", mappedBy="facility")
*/
protected $sports;
public function getSports()
return $this->sports;
public function setSports($sportsFacilities)
$this->sports = $sportsFacilities;
return $this;
public function addSports($sportsFacilities)
$this->sports = $sportsFacilities;
return $this;
class SportsFacility
/**
* @var integer $sportsFacilityId
*
* @ORM\Column(name="sportsFacilityId", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $sportsFacilityId;
/**
* Bidirectional - Many Sports are related to one Facility (OWNING SIDE)
*
* @ORM\ManyToOne(targetEntity="Sport", inversedBy="facilities"))
* @ORM\JoinColumn(name="sportId", referencedColumnName="sportId"))
*/
protected $sport;
/**
* Bidirectional - Many Facilities are related to one Sport (OWNING SIDE)
*
* @ORM\ManyToOne(targetEntity="Facility", inversedBy="sports"))
* @ORM\JoinColumn(name="facilityId", referencedColumnName="facilityId"))
*/
protected $facility;
public function getSportsFacilityId()
return $this->sportsFacilityId;
public function setSportsFacilityId($sportsFacilityId)
$this->sportsFacilityId = $sportsFacilityId;
return $this;
public function getSport()
return $this->sport;
public function setSport($sport)
$this->sport = $sport;
return $this;
public function getFacility()
return $this->facility;
public function setFacility($facility)
$this->facility = $facility;
return $this;
在我的 FacilityAdmin 课程中,我有:
protected function configureFormFields(FormMapper $formMapper)
$formMapper
->add('name')
->with('Sports')
->add('sports', 'sonata_type_collection',
array('by_reference' => false),
array(
'edit' => 'inline',
'inline' => 'table',
))
->end();
当我尝试添加新关系时,出现以下错误: “vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/EventListener/ResizeFormListener.php 第 88 行”中给出的“数组或\Traversable”类型的预期参数、“Clarity\CoachTimeBundle\Entity\SportsFacility”
【问题讨论】:
你确定在 SportsFacility 类的注解上关闭 @ORM\ManyToOne(targetEntity="Sport", inversedBy="facilities")) 的额外 ")" 吗? 我也有这个问题(但我在关系实体中添加了is_active
)。我看到很多人使用这个kind of solution 它可能对你有用(部分在我的情况下)
您找到解决问题的方法了吗?我有一个类似的问题:***.com/questions/15629225/…
您能添加您的 SportFacilityAdmin 吗?我不了解这里通常使用的技巧:* @ORM\OneToMany(targetEntity="SportsFacility", mappedBy="facility") */ protected $sports;你称它为 $sports,其实它应该是 $sportsFacilieties
【参考方案1】:
终于找到了我们的问题所在;在您的课堂设施中,您为奏鸣曲添加了缺失的方法,但它不应该做您认为应该做的事情(是的,是的):
class Facility
...
/**
* Alias for sonata
*/
public function addSports($sportFacility)
return $this->addSport($sportFacility);
我认为addSport()
是默认的学说生成方法,用于将SportsFacility
的新实例添加到集合中。
这是由于奏鸣曲如何生成添加新实体的方法与学说的做法不同:
//file: Sonata/AdminBundle/Admin/AdminHelper.php
public function addNewInstance($object, FieldDescriptionInterface $fieldDescription)
...
$method = sprintf('add%s', $this->camelize($mapping['fieldName']));
...
有人得到了the same problem,但文档没有发展
【讨论】:
以上是关于将 sonata_type_collection 用于一对多一关系的问题的主要内容,如果未能解决你的问题,请参考以下文章
Symfony 3 / sonata_type_collection 更改查询每个添加行
sonata_type_collection 设置页面加载时的默认行数
sonata_type_collection :从当前实体实例设置默认字段值
如何动态修改sonata_type_collection的子形式?