2012-10-02 38 views
4

我有一個相當常見的用例,我正在嘗試實現,但遇到Symfony Sonata Admin Bundle(ORM)的一些問題。我的模型在基礎設施和運動之間建立了關係,基於三個實體類:體育,設施和體育設施。我遵循示例http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#advanced-usage-one-to-many並在以下類中定義(僅限相關部分)。爲一對多關係使用sonata_type_collection的問題

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 I類有:

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(); 
} 

當我嘗試添加一個新的關係,我得到以下錯誤: 預期類型 「數組或\​​ Traversable的」 的說法,「清晰\ BusachTimeBundle \ Entity \ SportsFacility「在第88行」vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/EventListener/ResizeFormListener.php「

+0

你確定的額外「)」關閉@ORM \ ManyToOne(targetEntity =「Sport」,inversedBy =「facilities」))在SportsFacility類的註釋上? – Thomas

+0

我也有這個問題(但我在關係實體中添加了一個'is_active')。我看到很多人使用這種[解決方法](https://github.com/sonata-project/SonataDoctrineORMAdminBundle/issues/145)它可能適合你(部分在我的情況下) – DEY

+0

你有沒有找到一個修復你的問題?我有一個類似的ish問題:http://stackoverflow.com/questions/15629225/symfony2-1m-11-relationship-and-sonata-admin-form –

回答

3

終於找到我們出現問題的地方;在一流的設施,你增加了索納塔缺少方法,但它不應該這樣做,你認爲它應該做的(燁燁):

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但文檔沒有進化