实体映射不起作用

时间:2017-03-23 14:25:43

标签: doctrine-orm mapping entity zend-framework3

我试图将Doctrine2与ZF3一起使用。所有组件都已安装了作曲家。

当我尝试使用实体时,我有一个例外:

  

Doctrine \ Common \ Persistence \ Mapping \ MappingException

     

Class' Application \ Entity \ Concours'不存在

当我在Controller的操作中使用时抛出异常:

$concours = $this->entityManager->getRepository(Concours::class);

如果我使用的话,我会遇到同样的问题:

$entity = new Concours();

$entity = new \Application\Entity\Concours();

我真的不明白为什么......

感谢您的帮助...

我的配置:

config / local.php

use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySqlDriver;
return [
    'doctrine' => [
        'connection' => [
            'orm_default' => [
                'driverClass' => PDOMySqlDriver::class,
                'params' => [
                    'host'     => '127.0.0.1',                    
                    'user'     => 'xxxx',
                    'password' => 'xxxx',
                    'dbname'   => 'goch',
                ],
            ],            
        ],        
    ],
];

配置/ modules.config.php

return [
    'Zend\Cache',
    'Zend\Form',
    'Zend\InputFilter',
    'Zend\Filter',
    'Zend\Paginator',
    'Zend\Hydrator',
    'Zend\Router',
    'Zend\Validator',
    'DoctrineModule',
    'DoctrineORMModule',
    'Application',
];

对于应用程序模块,

模块/应用/配置/ module.config.php

namespace Application;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;


return [
    'router' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'application' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/application[/:action]',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::class =>  Controller\Factory\IndexControllerFactory::class,
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],

  'doctrine' => [
        'driver' => [
            __NAMESPACE__ . '_driver' => [
                'class' => AnnotationDriver::class,
                'cache' => 'array',
                'paths' => [__DIR__ . '/../src/Entity']
            ],
            'orm_default' => [
                'drivers' => [
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                ]
            ]
        ]
    ]  
];

我的控制器( module / Application / src / Controller / IndexController.php )是:

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Doctrine\ORM\EntityManager;
use Application\Entity\Concours;

class IndexController extends AbstractActionController {

     /**
   * Entity manager.
   * @var Doctrine\ORM\EntityManager
   */
  private $entityManager;

  // Constructor method is used to inject dependencies to the controller.
  public function __construct($entityManager) 
  {
    $this->entityManager = $entityManager;
  }


    public function indexAction() {
        return new ViewModel();
    }


    public function concoursAction(){
        $concours=array();

$concours = $this->entityManager->getRepository(Concours::class);
    // Render the view template
    return new ViewModel([
      'concours' => $concours
    ]);        

    }
}

以及相关的工厂

namespace Application\Controller\Factory;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Application\Controller\IndexController;

/**
 * This is the factory for IndexController. Its purpose is to instantiate the
 * controller.
 */
class IndexControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, 
                     $requestedName, array $options = null)
    {
        $entityManager = $container->get('doctrine.entitymanager.orm_default');

        // Instantiate the controller and inject dependencies
        return new IndexController($entityManager);
    }
}

我的所有实体类文件都在 module / Application / src / Application / Entity / 中, 这是Concours Entity Class:

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Concours
 *
 * @ORM\Table(name="Concours", uniqueConstraints={@ORM\UniqueConstraint(name="numero_UNIQUE", columns={"numero"})})
 * @ORM\Entity
 */
class Concours
{
    /**
     * @var integer
     *
     * @ORM\Column(name="ref", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $ref;
...
}

1 个答案:

答案 0 :(得分:2)

这是你的问题:

  

我的所有实体类文件都在   模块/应用/ SRC /应用/实体/

Zend 3使用PSR-4而不是PSR-0,这被认为是被删除的。您可以通过选中composer.json文件进行确认。您应该看到以下声明:

"autoload": {
    "psr-4": {
        "Application\\": "module/Application/src/",
    }
},

PSR-0 中,如果您在Foo\Bar中定义src/名称空间,则会在 PSR-4中查找src/Foo/Bar/{your_class}.php中的类会查看src/{your_class}.php

所以...来解决你的问题。移动Concours实体:

  

模块/应用/ SRC /应用/实体/

为:

  

模块/应用/ SRC /实体/