Search code examples
doctrine-ormmezzio

Zend Expressive: Internal Server Error 500 when activating the Service in Action


Please help to understand.
In the standard application CRUD, at connection of Service:
/src/App/Panel/Service/CategoriesService.php
in Action:
/src/App/Panel/Action/PanelCategoriesAction.php happens 500 error
link to repository: https://github.com/drakulitka/expressive.loc.git
Zend Expressive + Doctrine

Sorry for my English


Solution

  • You are mixing a few things up here. This should be your entity:

    <?php
    
    namespace App\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Table(name="categories")
     * @ORM\Entity(repositoryClass="App\Entity\Repository\CategoriesRepository")
     */
    class Categories
    {
    }
    

    In the doc comment annotation it tells Doctrine where to find the custom repository class. Doctrine loads it for you. The repositry doesn't need a constructor. Doctrine takes care of this for you.

    <?php
    
    namespace App\Entity\Repository;
    
    use App\Entity\Categories;
    use Doctrine\ORM\EntityRepository;
    
    class CategoriesRepository extends EntityRepository implements CategoriesRepositoryInterface
    {
        // No constructor here
    
        public function fetchAll()
        {
            // ...
        }
    }
    

    And then your factory looks like this:

    <?php
    
    namespace App\Panel\Factory;
    
    use Doctrine\ORM\EntityManager;
    use Interop\Container\ContainerInterface;
    use App\Entity\Categories;
    
    class CategoriesRepositoryFactory
    {
        /**
         * @param ContainerInterface $container
         * @return CategoriesRepository
         */
        public function __invoke(ContainerInterface $container)
        {
            // Get the entitymanager and load the repository for the categories entity
            return $container->get(EntityManager::class)->getRepository(Categories::class);
        }
    }
    

    In the config you use this:

    <?php
    
    return [
        'dependencies' => [
            'invokables' => [
            ],
            'abstract_factories' => [
            ],
            'factories' => [
                App\Entity\Repository\CategoriesRepositoryInterface::class => App\Panel\Factory\CategoriesRepositoryFactory::class,
            ],
        ],
    ];