【问题标题】:Cannot enable doctrine's entity manager无法启用学说的实体管理器
【发布时间】:2016-06-29 16:38:41
【问题描述】:

我想在我的项目中使用 Doctrine,但我无法使用 Entity Manager。 我已经创建了实体、存储库、配置文件和 dbconnect,但似乎没有正确完成。 你能检查一下这个代码吗?也许我错过了一些非常小的东西。

我的 dbconnect 文件(它在 init.php 中引导):

<?php
namespace Projekt\Config;

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = array("Entity");
$isDevMode = false;

// the connection configuration
$dbParams = array(
'driver'   => 'pdo_mysql',
'user'     => 'root',
'password' => '',
'dbname'   => 'projekt',
);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
$em = EntityManager::create($dbParams, $config);

我的存储库示例:

<?php

namespace Projekt\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * Message
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class Message extends EntityRepository
{
    public function getMessage($id)
    {
        $message = $this->find($id);
        return $message;

    }
    public function getAllMessages()
    {

    }
    public function createMessage()
    {

    }
    public function updateMessage()
    {

    }
    public function deleteMessage()
    {

    }
}

现在,当我尝试访问默认或自定义存储库方法时,我收到此错误:

Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::__construct(), 
called in F:\xampp\htdocs\mvc\app\Controllers\Messages.php 
on line 15 and defined in F:\xampp\htdocs\mvc\vendor\doctrine\orm\lib\Doctrine\ORM\EntityRepository.php on line 64

EntityRepository.php 中的第 64 行是一个声明 entitymanager 的 __construct 函数,但它似乎无法正常工作:

    public function __construct($em, Mapping\ClassMetadata $class)
{
    $this->_entityName = $class->name;
    $this->_em         = $em;
    $this->_class      = $class;
}

【问题讨论】:

    标签: php doctrine-orm


    【解决方案1】:

    我注意到的两件事:

    1. 你的路径是相对的。我不确定,但我总是使用 Entity 文件夹的完整路径。您可以使用 __DIR__ 轻松实现这一目标。根据您的命名空间,它应该看起来像:

      $paths = array(__DIR__ . "/../Repository");

    Doctrine 需要知道在哪里可以找到您的实体和存储库。根据您的命名空间,我认为您的存储库文件存在于名为“存储库”而不是“实体”的文件夹中。

    1. 您是否正确定义了实体类?您的 Repository 类对我来说看起来不错,但只有在您有一个有效的 Entity 类时它才能工作。

    您不应将存储库命名为“消息”。实体应命名为“Message”,存储库应命名为“MessageRepository”。

    【讨论】:

      猜你喜欢
      • 2013-05-03
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多