Andrew Rutter
Andrew Rutter

Reputation: 1297

Problems using a sonata_type_collection for a One-Many-One relation

I have a fairly common use case which I am trying implement but am running into some issues with the Symfony Sonata Admin Bundle (ORM). My model has a relationship between a Facility and a Sport which is based on three entity classes: Sport, Facility and SportsFacility. I followed the example http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#advanced-usage-one-to-many and defined in the following classes (relevant parts only).

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

In my FacilityAdmin class I have:

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

When I try to add a new relation, I get the following error: Expected argument of type "array or \Traversable", "Clarity\CoachTimeBundle\Entity\SportsFacility" given in "vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/EventListener/ResizeFormListener.php at line 88"

Upvotes: 4

Views: 5006

Answers (1)

DEY
DEY

Reputation: 1810

Finally found where we have the problem; in your class Facility you added the missing method for sonata, but it shouldn't do that you think it should do (yup yup) :

class Facility {
    ...
    /**
     * Alias for sonata
     */
    public function addSports($sportFacility) {
        return $this->addSport($sportFacility);
    }
}

I presume addSport() is the default doctrine generated method to add new instance of SportsFacilityto collection.

This is due to how sonata generate the method to add a new entity that is different how doctrine do :

//file: Sonata/AdminBundle/Admin/AdminHelper.php
public function addNewInstance($object, FieldDescriptionInterface $fieldDescription) 
{
   ...
   $method = sprintf('add%s', $this->camelize($mapping['fieldName']));
   ...
}

Someone got the same problem but the documentation didn't evolve

Upvotes: 3

Related Questions