我知道完全相同类型的问题已经在这里发布了很多次。但没有一个人解决了我的问题,真的很沮丧,谷歌搜索了 2 多天,仍然没有成功。我被困在这里无法继续我的项目。我已经说明了我最后通过谷歌找到的解决方案,但没有解决我的问题。
这是我的规格:在 wamp 服务器/php 5.4.3/apache 2.2.22 上使用 Zend Framework 2.1.5 和教义ORMModule。
这是我在 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
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;
}
}
Application/src/Application/Controller/UserController.php 中的 UserController
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,下面是 stak 跟踪和错误
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}
我找到的解决方案:
- 关闭 eaccelerator(我的 wamp 服务器上没有安装 eaccelerator,所以这不是问题)
- 将注释映射 @Entity 更改为 @ORM\Entity (使用两者进行检查,并没有解决我的问题 - 似乎
$config->newDefaultAnnotationDriver($path, true)
第二个参数simpleannotationreader
设置为 true。所以它会读取简单的注释 ex。@Entity
虽然我通过使用检查@ORM\Entity
仍然不起作用) - 我发现
annotationRegistry::somemethod()
如果这是解决方案我应该调用的某个地方,那么我应该从annotationRegistry
课堂上调用哪个方法以及调用的确切位置在哪里。
cmd命令:
./vendor/bin/doctrine-module orm:validate-schema
显示没有错误 '[Mapping] OK - 映射文件是正确的。[数据库] OK - 数据库架构与映射文件同步。- 但是执行此命令 -
`./vendor/bin/doctrine-module orm:info
抛出异常如下“根据当前配置,您没有任何映射的 orm 实体。如果你有实体或映射文件,你应该检查你的映射配置是否有错误。
就是这样。现在我能做的就是保持我的希望和耐心。感谢您查看我的问题。