Silex DoctrineORM:自定义实体存储库

时间:2016-07-11 12:59:58

标签: php doctrine-orm silex

尝试构建自定义实体存储库时遇到问题。

try {
    URL u = new URL("http://www.google.com");
    BufferedReader br = null;
    br = new BufferedReader(new InputStreamReader(u.openStream()));
    br.close();
} catch (IOException e) {
    e.printStackTrace();
}

"PHP message: PHP Fatal error: Class 'Doctrine\Orm\EntityRepository' not found in /home/user/projects/app/src/Application/Database/EntityRepository/UserRepository.php on line 8 文件中,我已经这样做了:

CustomRepository.php

然后在我的bootstrap文件中,这个方法是通过bootstrap类中的构造函数加载的。

<?php
    namespace Application\Database\EntityRepository;

    use Application\Bootstrap as Bootstrap;
    use Doctrine\Orm\EntityRepository;

    class UserRepository extends \Doctrine\Orm\EntityRepository
    {

        public function getPasswordByUsername($username)
        {
            $query = $app['orm.em']->createQuery('SELECT u.password FROM Application\Database\Entities\Users u WHERE (u.username = :username)');
            $query->setParameter('username', $username);

            return $query->getOneOrNullResult();
        }
    }

最后我的映射文件有这个:

use Silex\Provider\DoctrineServiceProvider as silexDocProv;
use Silex\Provider\SessionServiceProvider as silexSessProv;
use Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider as docProv;

private function initDatabase()
{
    $this->app->register(new silexDocProvj(), array(
        'db.options' => array(
            'driver' => 'pdo_mysql',
            'host'     => getenv('DBHOST'),
            'dbname'   => getenv('DBNAME'),
            'user'     => getenv('DBUSER'),
            'password' => getenv('DBPASS')
        )
    ));

    $this->app->register(new docProv(), array(
        'orm.em.options' => array(
            'mappings' => array(
                array(
                    'type' => 'yml',
                    'namespace' => 'Application\Database\Entities',
                    'path' => __DIR__ . '/../../app/Config/Mappings'
                )
            )
        )
    ));
}

1 个答案:

答案 0 :(得分:0)

命名空间区分大小写。

更改

Doctrine\Orm\EntityRepository

为:

Doctrine\ORM\EntityRepository

此外,

中不需要完整的命名空间
extends \Doctrine\Orm\EntityRepository

因为你已经通过

导入了这个类
use Doctrine\ORM\EntityRepository;

所以现在你可以这样做:

extends EntityRepository