0
votes

I know exactly same type of issue has been posted here many times. But not a single one solved my problem, really frustrated ,googling for more than 2 days finding solutions still with no success. I am stucked here not able to proceed ahead with my project. I have stated solutions I found through google in the last which didn't solve my problem.

Here are my specs: Using Zend Framework 2.1.5 with doctrineORMModule on wamp server / php 5.4.3 / apache 2.2.22.

Here are my doctrine configurations in Application/config/module.config.php

    // Doctrine Configurations
'doctrine' => array(
        'driver' => array(
                // defines an annotation driver with two paths, and names it `my_annotation_driver`
                'my_annotation_driver' => array(
                        'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                        'cache' => 'array',
                        'paths' => array(
                                __DIR__ . '/../src/Application/Model',
                        ),
                ),

                // default metadata driver, aggregates all other drivers into a single one.
                // Override `orm_default` only if you know what you're doing
                'orm_default' => array(
                        'drivers' => array(
                                // register `my_annotation_driver` for any entity under namespace `My\Namespace`
                                'Application\Model' => 'my_annotation_driver'
                        )
                )
        )
),
// End doctrine configurations

User Entity class in Application/src/Application/Model/User.php

namespace Application\Model;
/*
* @Entity
* @Table(name="user")
*/
class User
   {
/*
 * @Id 
 * @Column(type="integer")
 * @GeneratedValue(strategy="AUTO")
 */
private $id;

/*
 * @Column(type="string")
*/
private $username;

/*
 * @Column(type="string")
*/
private $password;

/*
 * @Column(type="string")
*/
private $email;

/*
 * @Column(type="string")
*/
private $name;

/*
 * @Column(type="string")
*/
private $profile_pic;

/*
 * @Column(type="string")
*/
private $cover_pic;

/*
 * @Column(type="integer")
*/
private $status;

/*
 * @Column(type="datetime")
*/
private $created_at;


public function getUsername()
{
    return $this->username;
}
}

UserController in Application/src/Application/Controller/UserController.php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class UserController extends AbstractActionController
{
public function IndexAction()
{
    $em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');

    $user = $em->find('Application\Model\User', 1);
    var_dump($user);

    exit;
}
}

Url - public/application/user -> throws exception ,below is the stak trace and error

Doctrine\ORM\Mapping\MappingException
File:
C:\wamp\library\Doctrine\ORM\Mapping\MappingException.php:216
Message:
Class "Application\Model\User" is not a valid entity or mapped super class.
Stack trace:
0 C:\wamp\library\Doctrine\ORM\Mapping\Driver\AnnotationDriver.php(87):                Doctrine\ORM\Mapping\MappingException::classIsNotAValidEntityOrMappedSuperClass('Application\Mod...')
1     C:\wamp\library\Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain.php(104):     Doctrine\ORM\Mapping\Driver\AnnotationDriver->loadMetadataForClass('Application\Mod...',     Object(Doctrine\ORM\Mapping\ClassMetadata))
2 C:\wamp\library\Doctrine\ORM\Mapping\ClassMetadataFactory.php(113):     Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain-    >loadMetadataForClass('Application\Mod...', Object(Doctrine\ORM\Mapping\ClassMetadata))
3     C:\wamp\library\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(302):     Doctrine\ORM\Mapping\ClassMetadataFactory-    >doLoadMetadata(Object(Doctrine\ORM\Mapping\ClassMetadata), NULL, false, Array)
4     C:\wamp\library\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(205):     Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory-    >loadMetadata('Application\Mod...')
5 C:\wamp\library\Doctrine\ORM\EntityManager.php(268): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('Application\Mod...')
6 C:\wamp\library\Doctrine\ORM\EntityManager.php(682): Doctrine\ORM\EntityManager->getClassMetadata('Application\Mod...')
7 C:\wamp\www\indians\module\Application\src\Application\Controller\UserController.php(23): Doctrine\ORM\EntityManager->getRepository('Application\Mod...')
8 C:\wamp\library\Zend\Mvc\Controller\AbstractActionController.php(83): Application\Controller\UserController->IndexAction()
9 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
10 C:\wamp\library\Zend\EventManager\EventManager.php(472): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
11 C:\wamp\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
12 C:\wamp\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
13 C:\wamp\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
14 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
15 C:\wamp\library\Zend\EventManager\EventManager.php(472): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
16 C:\wamp\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
17 C:\wamp\library\Zend\Mvc\Application.php(294): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
18 C:\wamp\www\indians\public\index.php(12): Zend\Mvc\Application->run()
19 {main}

Solutions I found:

  • turning off eaccelerator (eaccelrator is not installed on my wamp server, So this is not an issue)
  • change annotation mapping @Entity to @ORM\Entity (checked using both ,didn't solve my problem- seems like $config->newDefaultAnnotationDriver($path, true) second param simpleannotationreader is set to true.So it will read simple annotation ex. @Entity though I checked by using @ORM\Entity still doesn't work)
  • Some Place I found that I should call annotationRegistry::somemethod() if this is the solution then which method I shud call from annotationRegistry class and where's the exact place to call.

cmd commands:

  • ./vendor/bin/doctrine-module orm:validate-schema shows no error '[Mapping] OK - The mapping files are correct. [Database] OK - The database schema is sync with the mapping files.'
  • however executing this command - `./vendor/bin/doctrine-module orm:info throws exception as follows 'You do not have any mapped orm entities according to the current configuration. I you have entities or mapping files you should check your mapping configuration for errors.'

That's it .Now all I can do is keep my hope and patience. thanks for checking out my issue.

2
did you just change the annotations, or did you actually include the mapping class? use Doctrine\ORM\Mapping as ORM => @ORM\*. Furthermore, when validating schemata via the commandline, always make sure to delete the frikkin cache (when youre on windows) ;)Sam
@sam thanks for replying ..ofcourse I included it..just forgot to mention it here.. you mean clearing metadata-cache?Sumeet
Anyways why I am getting exception 'not a valid entity' while Application\Model\User resolves correctly to the class. As much I know entitymanager just needs an entity class with proper annotations.Sumeet
@Sumeet - the output of orm:info is telling you that doctrine[-orm-module] is not recognizing your entity. Your configuration and code looks pretty good, with the exception of what Sam said. Are you 100% sure that you've added use Doctrine\ORM\Mapping as ORM; at the top of your Model's class file, and converted all your annotations to have the ORM` prefix (so @Entity` becomes @ORM\Entity, etc, for every annotation)?timdev

2 Answers

3
votes

I found what was your problem: invalid comments. To describe Doctrine 2 ORM Entities you should annotations:

So for example - instead of:

/*
 * @Id 
 * @Column(type="integer")
 * @GeneratedValue(strategy="AUTO")
 */
private $id;

you should use:

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

As mentioned before - you should use annotations from Doctrine 2 ORM as follow (with @ORM\ at the beginning):

/**
 * @ORM\Column(type="string", length=31)
 */
private $username;

I encourage you to use following rule: all Entities store in subdirectory Entity, and functions operating on that entity - in subdirectory Model ( getUsers(), getUser(id), etc ).

Complete User Entity class in User/src/User/Entity/User.php (in my case):

namespace User\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Users table
 *
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @property int $id
 * @property string $username
 * @property string $password
 * @property string $email
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=31)
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=32)
     */
    private $password;

    /**
     * @ORM\Column(type="string", length=63)
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=63)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=127)
     */
    private $profile_pic;

    /**
     * @ORM\Column(type="string", length=127)
     */
    private $cover_pic;

    /**
     * @ORM\Column(type="integer")
     */
    private $status;

    /**
     * @ORM\Column(type="datetime")
     */
    private $created_at;


    public function __construct() {
    }


    /**
     * Magic getter to expose protected properties.
     *
     * @param string $property
     * @return mixed
     */
    public function __get($property)
    {
        return $this->$property;
    }

    /**
     * Magic setter to save protected properties.
     *
     * @param string $property
     * @param mixed $value
     */
    public function __set($property, $value)
    {
        $this->$property = $value;
        return $this;
    }
}

I also use plural form when I want to call some table.

E.g. users, articles, posts

And singular form for the Entity name (single record)

E.g. User, Article, Post

But this is just one of the patterns I am using.

BTW. Thanks for info about some usage of Doctrine 2 ORM console tool. I have never used ./doctrine-module orm:info, but only ./doctrine-module orm:schema-tool:create --dump-sql > database.sql

0
votes

Your problem is related to Docblock annotation "starter" characters:

A Docblock annotation is a C++ style comment starting with slash (/) and two asterisks (*). This "starter" characters are required, otherwise Doctrine won't recognize the annotation.

Change all comments to start with a /** and it will work.