N
N
Nikolai Gromov2017-02-16 08:33:41
Doctrine ORM
Nikolai Gromov, 2017-02-16 08:33:41

How to create an Entity in zf3 via command line?

Hello, I decided to learn ZF3. I watch video tutorials, though on ZF2, I found a book on the third zend. Reached creation of entities from a DB. In the video tutorials, entities are created through a .bat file on the command line. I don’t create them like that, in the comments to the video I found a code that does the same from the command line

doctrine-module orm:convert-mapping --namespace="Shop\Entity\\" --force --from-database annotation ./module/Shop/src/
and
doctrine-module orm:generate-entities ./module/Shop/src/ --generate-annotations=true

Doctrine installed from here created bootstrap.php
<?php

// bootstrap.php
require_once "../../vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Shop\Entity;?
$paths = array(Entity);?
$isDevMode = false;
// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '1',
    'dbname'   => 'shop',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);

where are the question marks, in the code I'm not sure (whether I specified the path correctly).
alias Shop\Entity is registered in my module\Shop\config\module.config.php
<?php
namespace Shop;

use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'doctrine' => [
        'driver' => [
            'shop_entity' => [
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => [
                    __DIR__. '/../src/Shop/Entity',
                ],
            ],
            'orm_default' => [
                'drivers' => [
                    'Shop\Entity' => 'shop_entity'
                ]
            ]
        ]
    ], 
    'router' => [
        'routes' => [
            'shop' => [
                'type' => Segment::class,
                'options' => [
                    'route'    => '/[:action/][:id/]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],
];

bootstrap.php is in \config\autoload\ and cli-config.php is in \config\
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;

// replace with file to your own project bootstrap
require_once '/autoload/bootstrap.php';

// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();

return ConsoleRunner::createHelperSet($entityManager);

So, when on the command line I enter
doctrine orm:convert-mapping --namespace="Shop\Entity\\" --force --from-database annotation ./module/Shop/src/

it tells me that the cli-config.php or config\cli-config.php file is missing and offers to use the sample that I have already written. Perhaps I messed up something with the addresses, even most likely it does not find this file again. And in the installation example in the section Setting up the Commandline Tool there is such a line
php vendor/bin/doctrine
why she does not understand, when I run it, she just gives me the contents of the file and that's it. And last question: How to create Entity in zf3 via command line?
I just found out that I don’t have the GetEntityManager() function anywhere, which is called in cli-config.php

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OrlandoST, 2017-05-25
@OrlandoST

No need to create any bootstrap.php
In global.php or local.php in the config, write the connection settings for Doctrine The
module is accessed along the path: ./vendor/doctrine/doctrine-module/bin/doctrine-module
In your case, like this:
. /vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping --namespace="Shop\Entity\\" --force --from-database annotation ./module/Shop/src/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question