3
votes

I want to generate Entities from an Existing Database by using Doctrine tools for reverse engineering

you can ask Doctrine to import the schema and build related entity classes by executing the following two commands.

1 $ php app/console doctrine:mapping:import AcmeBlogBundle annotation
2 $ php app/console doctrine:generate:entities AcmeBlogBundle

but now the doctrine detect only ManyToOne relation in many side only "ProviderCountry" table

if i need to add the ManyToMany relation i have to add the annotation by my hand by adding the follwing annotation

in Country.php add

/**
 * 
 * @var Provider $provider
 * 
 * @ORM\ManyToMany(targetEntity="Provider")
 * @ORM\JoinTable(name="provider_country",
 *      joinColumns={@ORM\JoinColumn(name="countryId", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="providerId", referencedColumnName="id")}
 *      )
 * */
private $providers;

in Provider.php add

  /**
     * @var Country $country
     * 
     * @ORM\ManyToMany(targetEntity="Country")
     * @ORM\JoinTable(name="provider_country",
     *      joinColumns={@ORM\JoinColumn(name="providerId", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="countryId", referencedColumnName="id")}
     *      )
     * */
    private $countrys;

so how can I generate Many-To-Many annotation by doctrine command [doctrine:mapping:import]

Thanks in advance.

2

2 Answers

2
votes

you can do this by add the following lines in

vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php

 $tables = array();
    $tables[] = $schemaManager->listTableDetails("country");
    $tables[] = $schemaManager->listTableDetails("provider_country");
    $tables[] = $schemaManager->listTableDetails("provider");
    $this->setTables($schemaManager->listTables(), $tables);

inside __construct

public function __construct(AbstractSchemaManager $schemaManager)

Note: That will override OneToMany annotation.

0
votes

I wouldn't recommend you to modify the vendor. You should modify your schema and generate the entities and the database after.

In your case I would modify the schema after you generated it from database

Symfony2 Jobeet - The Data Model

manyToMany:
    affiliates:
        targetEntity: Affiliate
        mappedBy: categories

manyToMany:
        categories:
            targetEntity: Category
            joinTable:
                name: category_affiliate
                joinColumns:
                    affiliate_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    category_id:
                        referencedColumnName: id