Sven
Sven

Reputation: 13296

Doctrine 2 in ZF2 application: Entities are not being found "No Metadata Classes to process."

In a Zend Framework 2 Project, Doctrine 2 is not able to find my Entities and I am not sure why. To me everything looks correct, but the php ./vendor/bin/doctrine-module orm:schema-tool:create command to create the database based on the entities results in No Metadata Classes to process..

Of course I made sure first that all modules are loaded, so it's not that easy ;-) I also already did some research on that problem and it seems that most of the time, it's namespace related, but again, I don't see anything wrong there. I also generated the autoload files.

Are you able to spot anything wrong in these files, why is Doctrine not able to find the Entities? Could it be related to something else than an error in my files?

In module.config.php, Doctrine is configured like this:

'doctrine' => [
    'driver' => [
      'Catalog_entities' => [
        'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
        'cache' => 'array',
        'paths' => [__DIR__ . '/../src/Catalog/Entity']
      ],
    ],

    'orm_default' => [
      'drivers' => [
        'Catalog\Entity' => 'Catalog_entities'
      ],
    ],
],

module.config.php has no namespace declaration since it's included into Module.php which has namespace Catalog; at the top.

The Entity at module/Catalog/src/Catalog/Entity/WorkEntity.php looks like this:

namespace Catalog\Entity;

use Doctrine\ORM\Mapping as ORM;

    /** @ORM\Entity */
    class Work {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @ORM\Column(type="integer")
         */
        protected $id;

        public function getId() {
          return $this->id;
        }

        public function setId($id) {
          $this->id = $id;
        }
}

Upvotes: 0

Views: 415

Answers (1)

Wilt
Wilt

Reputation: 44422

First fix @Crisp his comment. File names and class names should always correspond.

If that doesn't solve it you could check if you module.config.php is loaded correctly in your Module.php like this:

/**
 * @return array
 */
public function getConfig()
{
    return __DIR__ . '/../../config/module.config.php';
}

I would also suggest using the namespace constant so you can simply copy your configs to different modules without changing a thing and minimizing the risk of overwriting another config when you forget to change something. So like this:

'driver' => [
    __NAMESPACE__ . '_driver' => [
        'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
        'cache' => 'array',
        'paths' => [__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity']
    ],
    'orm_default' => [
        'drivers' => [
            __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
        ]
    ]
],

Upvotes: 1

Related Questions