简体   繁体   中英

Entity Mapping doesn't work

I'm trying to use Doctrine2 with ZF3. All components have been installed with composer.

When I try to use an Entity I have an exception :

Doctrine\\Common\\Persistence\\Mapping\\MappingException

Class 'Application\\Entity\\Concours' does not exist

The exception is thrown when I use in the Controller's action :

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

I have the same problem if i use :

$entity = new Concours();

or

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

I really don't understand why...

Thanks for help...

My configuration :

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',
                ],
            ],            
        ],        
    ],
];

config/modules.config.php

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

For Application Module,

module/Application/config/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'
                ]
            ]
        ]
    ]  
];

My Controller ( module/Application/src/Controller/IndexController.php ) is :

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
    ]);        

    }
}

And the associated Factory

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);
    }
}

All my Entities class files are in module/Application/src/Application/Entity/ , here is the 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;
...
}

Here is your problem:

All my Entities class files are in module/Application/src/Application/Entity/

Zend 3 uses PSR-4 instead of PSR-0 which is considered as depracated. You can confirm it by checking composer.json file. You should see following declaration:

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

In PSR-0 if you define Foo\\Bar namespace is achored in src/ it will look for class in src/Foo/Bar/{your_class}.php while in PSR-4 it will look in src/{your_class}.php .

So... to fix your problem. Move Concours entity from:

module/Application/src/Application/Entity/

to:

module/Application/src/Entity/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM