Search code examples
phpformssymfonydoctrinesymfony2-easyadmin

Easyadmin One-to-Many: not displaying form


With Easyadmin, Symfony3 and Doctrine I'm trying to display an embedded form for an Article to add Urls to itself.

However, the article form currently does not show the UrlType form, but instead text boxes which - what it seems - expect Ids of the Urls I would like to add.

enter image description here

I found that the following configuration should be working (according to this question):

       form:
            fields:
                [...]
                - { property: 'urls', type: 'collection', type_options: { entry_type: 'MyVendor\MyBundle\Form\UrlType', by_reference: false }  }

Article:

class Article
{
    [..]  

    /**
     * @ORM\OneToMany(targetEntity="Url", mappedBy="article", cascade={"persist", "remove"})
     */
    private $urls;

    public function __construct() {
        $this->urls = new ArrayCollection();
    }

    /**
     * Add url
     *
     * @param Url $url
     *
     * @return Article
     */
    public function addUrl(Url $url)
    {
        $url->setArticle($this);

        $this->urls[] = $url;

        return $this;
    }

    /**
     * Remove url
     *
     * @param Url $url
     */
    public function removeUrl(Url $url)
    {
        $this->urls->removeElement($url);
    }

    /**
     * Get url
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getUrls()
    {
        return $this->urls;
    }
}

Url:

class Url
{
   [..]  

    /**
     * @var Article
     *
     * @ORM\ManyToOne(targetEntity="Article", inversedBy="urls")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="article_id", referencedColumnName="id")
     * })
     */
    private $article;


    /**
     * Set article
     *
     * @param Article $article
     *
     * @return Url
     */
    public function setArticle(Article $article = null)
    {
        $this->article = $article;

        return $this;
    }

    /**
     * Get article
     *
     * @return Article
     */
    public function getArticle()
    {
        return $this->article;
    }
}

UrlType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('address', TextType::class);
    $builder->add('description', TextType::class);
}

I have checked, double checked and tried so many things in the code and searched google as well as stackoverflow.

How can I display fields to add new URLs to the Article?


Solution

  • Thanks to the friendly folks at EasyAdmin this issue has been resolved. It turns out that despite of using the fully qualified namespace to UrlType in YAML config, the type is overwritten with the Symfony UrlType.

    Solution: Rename MyVendor\MyBundle\Form\UrlType to for example MyVendor\MyBundle\Form\UrlEntityType

    This looks like a bug in either Symfony or EasyAdmin.