Sinisa Valentic
Sinisa Valentic

Reputation: 552

Zend Framework 2 + Doctrine2 - Entities not detected

Hi i used composer to create the ZF2 skeleton app. Installed doctrine-module, doctrine-orm-module etc. composer file below:

{ "name": "zendframework/skeleton-application", "description": "Skeleton Application for ZF2", "license": "BSD-3-Clause", "keywords": [ "framework", "zf2" ], "homepage": "http://framework.zend.com/", "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.2.*", "doctrine/doctrine-orm-module": "0.*", "doctrine/data-fixtures": "dev-master", "zendframework/zend-developer-tools": "dev-master", "doctrine/migrations": "dev-master", "bjyoungblood/bjy-profiler": "dev-master", "zendframework/zftool": "dev-master" } }

Added doctrine config to the module as follows:

'doctrine' => array( 'driver' => array( __NAMESPACE__.'_entities' => array( 'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver', //'cache' => 'array', 'paths' => array( __DIR__ . '/../src/'. __NAMESPACE__ .'/Entity', ) ), 'orm_default' => array( 'drivers' => array( __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_entities' ) ))),

And i added my entity to: module/Application/src/Application/Entity/User.php

But when i run: php zf.php orm:info

i get the following message:

[Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors.

If i try

php zf.php orm:schema-tool:create

i get:

No Metadata Classes to process.

How can i get Doctrine to generate my database? What am i doing wrong?

Edit: Here is the entity code as requested in the comments:

<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity */
class User {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /** @ORM\Column(type="string") */
    protected $fullName;
}

Small update,

i am actually running this inside of a virtual machine. The folder where the files are is mounted from the host. If i copy files to tmp folder, it works just fine. If i run php zf.php orm:info in the mounted folder i get an error. Virtualisation used is Paralles 8, host OS is OSX, and guest is Debian 7. I am working in /media/psf/ mount.

Upvotes: 3

Views: 2088

Answers (2)

Sinisa Valentic
Sinisa Valentic

Reputation: 552

The issue was with outdated parallels tools. After doing some investigation, i found out there is a ne version of parallels tools available.

Manual update of the tools, and restart of the virtual machine solved the problem. There was nothing wrong even with the original code, and it all worked fine after the update. I guess there was a bug in parallels, but i did not find any reference about it online.

Thank you for your help.

Upvotes: 2

jmleroux
jmleroux

Reputation: 947

The __NAMESPACE__ directive in your doctrine config must be a problem : your config probably do not use a namespace and so the __NAMESPACE__ will resolve to the main namespace.

You can try a config like this one :

'doctrine' => array(
    'driver' => array(
        'application_driver' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'array',
            'paths' => array(
                'module/Application/src/Application/Entity',
            ),
        ),

No need to use __DIR__, you can use the application root as ZendApplication is chrooted (in public/index.php : chdir(dirname(__DIR__)); ).

Finally, you can also specify the table name for your Entity and the getters/setters for mapped columns :

<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/** 
 * @ORM\Entity 
 * @ORM\Table(name="user")
 */
class User {
    /**
     * @var int
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;

    /** 
     * @var string
     * @ORM\Column(type="string") 
     */
    protected $fullName;

    /** 
     * @return int 
     */
    public function getId()
    {
        return $this->id;
    }

    /** 
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /** 
     * @return string
     */
    public function getFullName()
    {
        return $this->fullName;
    }

    /** 
     * @param string $fullname
     */
    public function setFullName($fullname)
    {
        $this->fullName= $fullname;
    }
}

Upvotes: 2

Related Questions