Symfony, Sonata, Mongodb dealing with inheritence

I have a class impression, and a class like. class like extends impressions I will show u both classes below:

<?php


namespace AppDocument;
use DoctrineODMMongoDBMappingAnnotations as MongoDB;
use DoctrineODMMongoDBMappingAnnotationsDefaultDiscriminatorValue;
use DoctrineODMMongoDBMappingAnnotationsDiscriminatorField;
use DoctrineODMMongoDBMappingAnnotationsDiscriminatorMap;
use DoctrineODMMongoDBMappingAnnotationsInheritanceType;



/**
 * Class Impressions
 * @MongoDBDocument 
 * @inheritanceType("SINGLE_COLLECTION")
 * @DiscriminatorField("type")
 * @DiscriminatorMap({"impressions"=Impressions::class, "like"=Like::class, "comment"=Comment::class, "view"=View::class})
 * @DefaultDiscriminatorValue("impressions")
 */
class Impressions
{
    /**
     * @MongoDBId()
     */
    private $id;
    

    /**
     * @MongoDBReferenceOne(targetDocument=User::class, inversedBy="impression")
     */
    private $user;


    public function __toString()
    {
        return $this->type;
    }
    
    /**
     * @MongoDBReferenceOne(targetDocument=Review::class, inversedBy="impression")
     */
    private $review;

    //Article should be added with the relation
    /**
     * @MongoDBReferenceOne(targetDocument=Article::class, inversedBy="impressions")
     */
    private $article;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Get the value of article
     */
    public function getArticle(): ?Article
    {
        return $this->article;
    }

    /**
     * Set the value of article
     *
     * @param $article
     * @return  self
     */
    public function setArticle($article)
    {
        $this->article = $article;

        return $this;
    }

}

class Like

<?php

namespace AppDocument;


use DoctrineODMMongoDBMappingAnnotations as MongoDB;

/**
 * @MongoDBDocument
 */
class Like extends Impressions
{
    /**
     * @MongoDBReferenceOne(targetDocument=Article::class, inversedBy="likes")
     */
    private $article;

    public function __toString()
    {
        return (string) $this->getId();
    }

    /**
     * Get the value of article
     */ 
    public function getArticle(): ?Article
    {
        return $this->article;
    }

    /**
     * Set the value of article
     *
     * @return  self
     */ 
    public function setArticle($article)
    {
        $this->article = $article;

        return $this;
    }
}

I want to show in the sonata list that the type of impression is like (i also have view and comment but all are the same

Sonata Admin of Impressions

final class ImpressionsAdmin extends OBSAdmin
{
    protected function configureFormFields(FormMapper $form)
    {

       $subject = $this->getSubject(); 

       $form->add('article', ModelListType::class, [
        'btn_add' => 'Add Article',
        'btn_list' => 'button.list',
        'btn_delete' => false,
        'btn_catalogue' => 'SonataNewsBundle',
     ]);    

        if ($subject instanceof Comment){
            $form->add('text', TextType::class);
        }
    }
    

    protected function configureListFields(ListMapper $list)
    {
       $list->add('article')
            ->add('text')
            ->add('type');
    }

    protected function configureShowFields(ShowMapper $show)
    {
        $show->add('article');
    }

    public function getRoutePrefix()
    {
        return "impressions_api";
    }

    public function getJSONFields($context)
    {
        return ['id','type|object|admin.impressions', 'article|ONE_TO_ONE|admin.article', 'text'];
    }

    
    public function toString($object)
    {
        return $object instanceof Impressions
               ? $object->__toString()
               : 'type';
    }
}

here in the sonata list the column for type returns NULL, how can i make it return the actual option (like, view, or comment)

Source: Symfony Questions

Was this helpful?

0 / 1

Leave a Reply 0

Your email address will not be published. Required fields are marked *