Doctrine 2 and Symfony 2 Dynamic Target

I want the dynamic mapping of objects on one object to be used by other objects. For example, I have a File object that will store the type MIME, association key, nameetc., and also entity_idthat will contain an identifier for the object to which it belongs. The mapping keywill determine the class, since this file object will be many-to-many. Thus, the object targetEntityfor the file is not fixed. How to achieve this?

File entity

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * File
 *
 * @ORM\Entity
 */
class File {
    //.... Other mapping properties

    /**
     * @ORM\ManyToOne(targetEntity="SuperClass", inversedBy="files")
     * @ORM\JoinColumn(name="entity_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $entity;
}

Product

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Product
 *
 * @ORM\Entity
 */
class Product extends SuperClass {
    //.... Other mapping properties

    /**
     * @ORM\OneToMany(targetEntity="File", mappedBy="entity")
     */
    protected $files;
}

, Product, , getFiles() . , , , ?

+4
2

, , - , .

-, : , , , . , ( )!

: () . , . , , ( , [ ], (, mime ), , .

, . , ( , ofcorse). .

, doctrine2, , ( ) .

+1

, ManyToMany Unidirectional OneToMany Unipirectional. , , . - , . ,

CREATE TABLE Product (
    id INT AUTO_INCREMENT NOT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;

CREATE TABLE Feature (
    id INT AUTO_INCREMENT NOT NULL,
    product_id INT DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;

ALTER TABLE Feature ADD FOREIGN KEY (product_id) REFERENCES Product(id);

, SuperClass, , , , - .

0

All Articles