Symfony 2.1, SonataAdminBundle And ManyToMany

1,703 views
Skip to first unread message

Vince Maj

unread,
Sep 2, 2012, 11:15:07 AM9/2/12
to sonata...@googlegroups.com
Hi,

First, i'm new to symfony 2 and Sonata, so it's maybee a dumb question.. But i've searched without finding an answer.

I've got 2 Entities, User and Article, Many users can collaborate on many articles.

in User :

    /**
     * Bidirectional - Many users have Many articles (OWNING SIDE)
     *
     * @ORM\ManyToMany(targetEntity="Article", inversedBy="userArticles")
     * @ORM\JoinTable(name="user_articles",
     *   joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *   inverseJoinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="id")}
     * )
     */
    private $articles;

 and in Article :

    /**
     * Bidirectional - Many articles are authored by many users (INVERSE SIDE)
     *
     * @ORM\ManyToMany(targetEntity="User", mappedBy="articles")
     */
    private $userArticles;


When i try to add the field in SonataAdminBundle, like this :

        $formMapper->add('userArticles', 'sonata_type_collection', array(
                'by_reference' => false
            ), array(
                'expanded' => true,
                'edit' => 'inline',
                'inline' => 'table',
                'sortable'  => 'name'
            ));

I've got an error :

INVALID MODE : s50436e8e06884_userArticles - type : sonata_type_collection - mapping : 8


What did i forgot or did wrong ?


While i'm here.. ;)..

I'm trying to add Medias to the Article, like this :
     /**
     * @var ArrayCollection $media
     *
     * @ORM\OneToMany(targetEntity="Application\Sonata\MediaBundle\Entity\Media", mappedBy="article")
     */
    private $media;


This works in the admin with this code :
        $formMapper->add('media', 'sonata_type_collection', array(
                'by_reference' => false
            ), array(
                'edit' => 'inline',
                'inline' => 'table',
                'sortable'  => 'name',
                'link_parameters' => array()
            ));

but when i click the "add" button, i've got an error (ajax) :

Property "media" is not public in class "Vince\CmsBundle\Entity\Article", nor could adders and removers be found based on the guessed singulars: Medion, Medium (provide a singular by suffixing the property path with "|{singular}" to override the guesser). Maybe you should create the method "setMedia()"


And i don't have the list of Media already in DB. I've got 2 Images, added with SonataMediaBundle

Hmm.. I must have forgot something, but what ? Thanks for any help.

Vince Maj

unread,
Sep 2, 2012, 6:35:06 PM9/2/12
to sonata...@googlegroups.com
If i understand right, i need to create a "Form Type" to use in the collection ?

i've tried this :

        use Vince\CmsBundle\Form\Type\UserType;

(...)

        $formMapper->add('userArticles', 'sonata_type_collection', array(
                'type' => new UserType(),
                'allow_add' => true,
                'by_reference' => false,

            ), array(
                'expanded' => true,
                'edit' => 'inline',
                'inline' => 'table',
                'sortable'  => 'name'
            ));




And the UserType.php looks like this :


<?php

namespace Vince\CmsBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class UserType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('name');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Vince\CmsBundle\Entity\User',
        );
    }

    public function getName()
    {
        return 'user';
    }
}


But i've got another error ..

Fatal error: Declaration of Vince\CmsBundle\Form\Type\UserType::buildForm() must be compatible with that of Symfony\Component\Form\FormTypeInterface::buildForm() in /home/****/sites/sf2/html/src/Vince/CmsBundle/Form/Type/UserType.php on line 26

Vince Maj

unread,
Sep 2, 2012, 6:53:20 PM9/2/12
to sonata...@googlegroups.com
ok, corrected my UserType like this :


<?php

namespace Vince\CmsBundle\Form\Type;


use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(

            'data_class' => 'Vince\CmsBundle\Entity\User',
        ));

    }

    public function getName()
    {
        return 'user';
    }
}

Working..

But still the

User Articles *
INVALID MODE : s5043e33daa859_userArticles - type : sonata_type_collection - mapping : 8



Le dimanche 2 septembre 2012 17:15:07 UTC+2, Vince Maj a écrit :

Gabriel Richards

unread,
Jan 12, 2013, 3:12:46 AM1/12/13
to sonata...@googlegroups.com, vince...@gmail.com
I have the same problem... did you ever find a solution to getting many to many to work with sonata_type_collection?

For now, I am just using a stock Symfony multi-select.

G
Reply all
Reply to author
Forward
0 new messages