0

I´m trying to import a database to start a ZF3 PHP project using Doctrine.

I´ve read this tutorial on how to create entities from the database and issued the following command:

$ cd myproject
$ ./vendor/bin/doctrine-module mapping:import

And got this error:

[Symfony\Component\Console\Exception\CommandNotFoundException]
There are no commands defined in the "mapping" namespace.

The command:

$ ./vendor/bin/doctrine-module list

lists the available commands. Only orm:, migration: and dbal: are listed, no mapping: commands at all.

Am I missing some installation phase here ?

Mendes
  • 17,489
  • 35
  • 150
  • 263

1 Answers1

1

You're not missing :mapping as there is no mapping command within the Doctrine Tools. The Doctrine Symfony Bundle probaly made its own command and forwards it to another command within the Doctrine Tools. (Some one with some Symfony knowledge able to confirm?). As for ZF using php /vendor/bin/doctrine-module list lists all the Doctrine Tool commands. You should take a look at the Doctrine ORM Tools.

As described within the documentation you should probaly use the Reverse engineering. As I did understood that you want to create models/entities from an existing database.

But before reverse engineering your database to models you should consider the following as stated by Doctrine:

Reverse Engineering is a one-time process that can get you started with a project. Converting an existing database schema into mapping files only detects about 70-80% of the necessary mapping information. Additionally the detection from an existing database cannot detect inverse associations, inheritance types, entities with foreign keys as primary keys and many of the semantical operations on associations such as cascade.

And notice the following as this might be important as you have to check what has been created and correct it when needed.

Reverse Engineering is not always working perfectly depending on special cases. It will only detect Many-To-One relations (even if they are One-To-One) and will try to create entities from Many-To-Many tables. It also has problems with naming of foreign keys that have multiple column names. Any Reverse Engineered Database-Schema needs considerable manual work to become a useful domain model.

And don't use Symfony 3 documentation to help you out with Zend Framework integration of Doctrine, as both got their own ways (Bundles vs Modules). Sure some things within the Symfony Bundle or Zend Module are matching but that is just based on the tools provided by Doctrine itsself.

Edited due to comments

As stated in the comments, it is not really clear which command replaces the Symfony command: $ php bin/console doctrine:mapping:import --force AcmeBlogBundle xml. Within your ZF2/3 application you don't have this function to generate the mapping for you instantly within the Doctrine (ORM) Module. As in Symfony you can specify the Bundle which you want to generate the entities for. This is a specific thing build within Symfony. To mimic this you've do it yourself within a Zend Framework application.

Within your ZF application you've to create your own task to do so, so you've to create this job on something where you've got access to your EntityManager.

    /** @var \Doctrine\Orm\EntityManager $em */
    $em = $this->getEntityManager();
    $em->getConfiguration()->setMetadataDriverImpl(
        new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
            $em->getConnection()->getSchemaManager()
        )
    );

    $cmf = new \Doctrine\Orm\Tools\DisconnectedClassMetadataFactory();
    $cmf->setEntityManager($em);
    $metadata = $cmf->getAllMetadata();

    $cme = new \Doctrine\Orm\Tools\Export\ClassMetadataExporter();
    //$_exporterDrivers = array(
    //    'xml' => 'Doctrine\ORM\Tools\Export\Driver\XmlExporter',
    //    'yaml' => 'Doctrine\ORM\Tools\Export\Driver\YamlExporter',
    //    'yml' => 'Doctrine\ORM\Tools\Export\Driver\YamlExporter',
    //    'php' => 'Doctrine\ORM\Tools\Export\Driver\PhpExporter',
    //    'annotation' => 'Doctrine\ORM\Tools\Export\Driver\AnnotationExporter'
    //);
    $exporter = $cme->getExporter('xml', '/path/to/export/xml');
    $exporter->setMetadata($metadata);
    $exporter->export();

For example in your Module.php in your Application module. Add this method: public function onBootstrap(MvcEvent $event)

class Module
{

    public function onBootstrap(\Zend\Mvc\MvcEvent $event)
    {
        $entityManager = $event->getApplication()->getServiceManager()->get('doctrine.entitymanager.orm_default');

        // code block above
        // remove this code when export is done
    }
}

Now that we've got your metadata within the XML file or the file type you specified, you can use the following command to create the entities:

$ php doctrine orm:convert-mapping --from-database xml /path/to/mapping-path-converted-to-xml

Doctrine-module users:

$ php /vendor/bin/doctrine-module orm:convert-mapping --from-database xml /path/to/mapping-path-converted-to-xml
Kwido
  • 1,382
  • 8
  • 21
  • Good theory Kwido, and the risks of using Revese Enginnering are known (the text you mention are part of the link in my original post). Not being able to use the `mapping` command to do the reverse engineering is the problem that I need to solve, and I don´t see a hint on how to do it.... – Mendes Nov 25 '16 at 11:04
  • @Mendes I did link to the Reverse Engineering, where the command is, which should replace the Symfony command you're using for your Zend application. Well you need to follow the steps at the reverse engineering to solve it. Let Doctrine read your database and export that to a file. And based on that file, which contain all the entity mapping, you could run: `$ php doctrine orm:convert-mapping --from-database yml /path/to/mapping-path-converted-to-yml`. Well I can repeat it in the answer but making it a bit of a duplication. Will update the answer a bit. – Kwido Nov 25 '16 at 11:11
  • 2
    I´ll upvote that this helped me to find the way, but the command I need is: `php ./vendor/bin/doctrine-module orm:convert-mapping --from-database annotation ./generatedmodelspath`. I got php files that still needs to be worked, but its fine. Thanks to [this post](http://stackoverflow.com/q/14968924/2697571) – Mendes Nov 25 '16 at 11:43
  • Okay cool you've found the way on how to do it. Just learned myself some new things aswell with this. Will update the command for the doctrine-module users. – Kwido Nov 25 '16 at 11:49