EntityManager不使用psr-4命名空间加载存储库

问题描述 投票:0回答:1

我正在尝试找到我在数据库中的所有用户,当我试图获取存储库时,学说映射失败,因为它无法找到所需的内容。

我在我的代码项目中有这个命名空间。

composer.json

  "autoload" : {
    "psr-4" : {
      "Olive\\Todo\\" : "src/",
      "Olive\\Todo\\Exceptions\\" : "exceptions/",
      "Olive\\Todo\\Tests\\" : "tests/"
    }
  }

当我试图让存储库找到所有用户时,它失败了。

指数

/**
 * Get Users array
 *
 * @param EntityManager   $entityManager
 *
 * @return  array
 **/
function findUser( $entityManager )
{
  $ret = [];

  $userRepository = $entityManager->getRepository('User');

  return $ret;
}

这是我在尝试执行此代码时遇到的错误。

致命错误:未捕获的Doctrine \ Common \ Persistence \ Mapping \ MappingException:/srv/www/simple-project/vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/MappingException.php中不存在类'User' :93堆栈跟踪:

这就是存储库本身。

UserRepository.php

<?php

namespace Olive\Todo\User;

use Doctrine\ORM\EntityRepository;
use Olive\Todo\User\User;

/**
 * @author  Ismael Moral <[email protected]>
 **/
class UserRepository extends EntityRepository
{
  /**
   * Get all users
   *
   * @return  array
   **/
  public function getUser( $maxResults = 50 )
  {
    $dql = "SELECT u FROM user u";

    $query = $this->getEntityManager()->createQuery( $dql );
    $query->setMaxResults( $maxResults );

    return $query->getResult();
  }
}

我想我需要在doctrine中设置一些配置参数才能加载这个类。

我在脚本的顶部导入了类,并且还更改了get repository方法。现在我又有一个错误。

  $userRepository = $entityManager->getRepository( User::Class ); 

这是我应用更改时遇到的新错误。

致命错误:未捕获BadMethodCallException:未定义的方法'getUser'。方法名称必须以findBy,findOneBy或countBy开头!在/srv/www/simple-project/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:238堆栈跟踪:

0 /srv/www/simple-project/index.php(78):Doctrine \ ORM \ EntityRepository - > __ call('getUser',Array)

1 /srv/www/simple-project/index.php(123):findUser(Object(Doctrine \ ORM \ EntityManager),Array)

在第238行的/srv/www/simple-project/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php中抛出2 {main}

提前致谢。

php doctrine
1个回答
1
投票

您必须使用命名空间类而不是类名。所以:

$userRepository = $entityManager->getRepository( 'Olive\Todo\User\User' );

你也可以使用这种语法,如果php已经通过我个人使用的“导入”它:

$userRepository = $entityManager->getRepository( User::class );
© www.soinside.com 2019 - 2024. All rights reserved.