How to generate Doctrine entities From Database and Use PSR-4 Autoloading?

家住魔仙堡 提交于 2020-01-04 05:43:25

问题


Using Doctrine 2.5 with PSR-4 autoloading and converting an already designed database schema to entity classes (annotations). The problem is getting the exported files in the correct directory structure.

composer.json

{
    "autoload": {
        "psr-4": {
            "Application\\": "src/"
        }
    },
    "require": {
        "doctrine/orm": "^2.5"
    }
}

orm:convert-mapping

vendor/bin/doctrine orm:convert-mapping \
    --namespace='Application\Entity\' \
    --force \
    --from-database  \
    annotation \
    src/

Running this command will add an Application directory in src/. The generated class file has the correct namespace but in the wrong directory for PSR-4 standard.

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;


}

Is there a way to solve this without a secondary command?


回答1:


I don't see any option from doctrine cli for this needed. I just see the solution to modify the directory structure from your Application module. Here I modify the composer.json

{
    "autoload": {
        "psr-4": {
            "Application\\": "src/Application/"
        }
    },
    "require": {
        "doctrine/orm": "^2.5"
    }
}

All Application module source code will be put on src/Application not src/ anymore. So, when doctrine cli create directory Application/Entity in src, it will match with yor psr-4 Autoloader.



来源:https://stackoverflow.com/questions/44504058/how-to-generate-doctrine-entities-from-database-and-use-psr-4-autoloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!