Search code examples
symfonymany-to-manysonata-admin

Sortable Sonata Type Model in Admin


Did someone tried the tutorial about Sortable Sonata Type Model in Admin. I've followed it step by step without missing anything (I'm pretty sure) but can't get a good result at the end.

Basically what I'm trying to do is : I have 3 entities, Article, Tag and ArticleTag (eq to User, Expectation and UserHasExpectation in the tutorial)

Everything seems good until the UserHasExpectationAdmin:

protected function configureFormFields(FormMapper $formMapper){
// ...

$formMapper
    ->add('userHasExpectations', 'sonata_type_model', array(
        'label'        => 'User\'s expectations',
        'query'        => $this->modelManager->createQuery('UserBundle\Entity\Expectation'),
        'required'     => false,
        'multiple'     => true,
        'by_reference' => false,
        'sortable'     => true,
    ))
;

$formMapper->get('userHasExpectations')->addModelTransformer(new ExpectationDataTransformer($this->getSubject(), $this->modelManager));}

I think an attribute 'class' => 'UserBundle\Entity\Expectation' should be added to 'userHasExpectations' field else Symfony says that it's an invalid value.

Then the other problem is in the dataTransformer:

It launch me the error:

Attempted to call an undefined method named "create" of class "Main\CoreBundle\Form\DataTransformer\TagDataTransformer"

I think a use statement should be added but I don't know which one. More over, suppose I have the right use statement I don't realize what the writer is aiming to do, if it's creating UserHasExpectation records why don't he add a userHasExpectations->setUser($this->User) ???

Also I want to add after "vardumping" $this->Subject before :

$formMapper->get('userHasExpectations')->addModelTransformer(new ExpectationDataTransformer($this->getSubject(), $this->modelManager));

It seems to have a proper Entity Object with all fields on NULL values...


Solution

  • FINALLY SOLVED IT!

    So, the code of the tutorial contains many...mistakes In spite of trying to create 'userHasExpectation' in the DataTransformer we just return the object userHasExpectation in the reverse DataTransformer then we create our records in the postPersist and postUpdate of our Admin Class that way :

    /**
     * {@inheritdoc}
     */
    public function postUpdate($object)
    {
        $position    = 0;
        $uniqId      = $this->getUniqId();
        $request     = $this->getRequest()->get($uniqId);
    
        $qb   = $this->modelManager->createQuery('MainCoreBundle:ArticleTag', 'at');
    
        $TagsToRemove = $qb->where('at.article = :article')
                            ->setParameter('article', $object)
                            ->getQuery()
                            ->getResult();
    
        foreach ($TagsToRemove as $Tag) {
            $this->modelManager->delete($Tag);
        }
    
        foreach($request["article_tags"] as $tag)
        {
            $Tag = $this->modelManager->find('MainCoreBundle:Tag', $tag);
    
            $article_tags = new ArticleTag;
            $article_tags->setTag($Tag);
            $article_tags->setArticle($object);
            $article_tags->setPosition($position++);
    
            $this->modelManager->create($article_tags);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function postPersist($object)
    {
        $position    = 0;
        $uniqId      = $this->getUniqId();
        $request     = $this->getRequest()->get($uniqId);
    
        foreach($request["article_tags"] as $tag)
        {
            $Tag = $this->modelManager->find('MainCoreBundle:Tag', $tag);
    
            $article_tags = new ArticleTag;
            $article_tags->setTag($Tag);
            $article_tags->setArticle($object);
            $article_tags->setPosition($position++);
    
            $this->modelManager->create($article_tags);
        }
    }
    

    Hope this will help Somebody who has the same trouble. @Sonata-admin-team : I hope you will read this and have time to update the tutorial in question.

    Thanks, Epixilog