OneToMany <=> ManyToOne in MappedSuperClass

I can resume my schema like this:
schema bdd
Abstract Block is a single table ineritance entity…
Abstract struture & abstract graph is supperclass

( i have multiple structure and graph … i just represente only one for the issue )

i want to add a collection of GraphDatum in every graph ...

in first place a try to add the collection in the Abstract graph but doctrine say :

It is illegal to put an inverse side one-to-many or many-to-many association on mapped superclass 'AppEntityMariaDbBlockGr
  aphAbstractGraph#graphData'.

So i try to make a trait and include my trait in all graph entity, but now a have a invalid mapping ( think because a referer to a abstract and not the final class :/ ? )

graphDataTrait:

trait graphDataTrait
{
    /** @var GraphDatum[]|ArrayCollection */
    protected $graphData;

    /**
     * Extend Constructor
     */
    private function initGraphData()
    {
        $this->graphData = new ArrayCollection();
    }

    /**
     * @param ClassMetadata $metadata
     */
    static private function loadMetadataGraphData(ClassMetadata $metadata)
    {
        $builder = new ClassMetadataBuilder($metadata);
        $builder
            ->createOneToMany('graphData', GraphDatum::class)
                ->mappedBy('graph')
                ->fetchLazy()
            ->build()
        ;
    }

    // Adder / remover , ect ... 

SimplePie:


class SimplePie extends AbstractGraph
{
    use graphDataTrait;

    /**
     * SimplePie constructor.
     */
    public function __construct()
    {
        parent::__construct();
        if ( method_exists($this, 'initGraphData' )) {
            $this->initGraphData();
        }
    }

    /**
     * @param ClassMetadata $metadata
     */
    public static function loadMetadata(ClassMetadata $metadata)
    {
        self::loadMetadataGraphData($metadata);
    }
    // ....

GraphDatum:

class GraphDatum
{
    /** @var int */
    protected $id;

    /** @var AbstractGraph */
    protected $graph;

    /** @var string */
    protected $name;

    /** @var string */
    protected $color;

    /** @var bool */
    protected $ref = false;

    /** @var AbstractComputed */
    protected $computed;

    /**
     * @param ClassMetadata $metadata
     */
    public static function loadMetadata(ClassMetadata $metadata)
    {
        $builder = new ClassMetadataBuilder($metadata);
        $builder
            ->setTable('graph_datum')
            ->setCustomRepositoryClass(AbstractServiceEntityRepository::class)
            ->createField('id', Types::INTEGER)
                ->makePrimaryKey()
                ->generatedValue()
            ->build()
            ->createField('name', Types::STRING)
                ->length(255)
            ->build()
            ->createField('color', Types::STRING)
                ->length(255)
            ->build()
            ->createField('ref', Types::BOOLEAN)
            ->build()
            ->createManyToOne('computed', AbstractComputed::class)
                ->inversedBy('graphData')
            ->build()
            ->createManyToOne('graph', AbstractGraph::class)
                ->inversedBy('graphData')
            ->build()
        ;
    }
    // Getter / Setter / ect ...

So what is the right approache to do this ?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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