我正在尝试将存储库注入服务中,为此我将以这种方式定义services.yml
文件:
parameters:
user.repository.class: App\UserBundle\Repository\UserRepository
user_repository.factory_argument: App\UserBundle\Usuario
user_repository.factory_argument2: Doctrine\ORM\Mapping\ClassMetadata
user.service.class: App\UserBundle\Services\UserServiceImpl
services:
app_user.registration.form.type:
class: App\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: app_user_registration }
login_listener:
class: 'App\UserBundle\Listener\LoginListener'
arguments: ['@security.context', '@doctrine', '@service_container']
tags:
- { name: 'kernel.event_listener', event: 'security.interactive_login' }
app.user.repository:
class: "%user.repository.class%"
factory-service: doctrine.orm.entity_manager
factory-method: getRepository
arguments: [%user_repository.factory_argument%, %user_repository.factory_argument2%]
user.service:
class: "%user.service.class%"
arguments: [ @app.user.repository ]
问题出在app.user.respository
参数定义中:arguments: [%user_repository.factory_argument%, %user_repository.factory_argument2%]
我收到错误:
PHP Catchable致命错误:传递给Doctrine \ ORM \ EntityRepository :: __ construct()的参数2必须是Doctrine \ ORM \ Mapping \ ClassMetadata的实例,给定字符串
我正在关注这个post,它实际上以不同的方式定义了这些参数,但这对我不起作用,也许它是针对先前版本的symfony或doctrine完成的。
修改
在'factory_service`中有一个拼写错误,如评论中所述,我仍然会绕过同样的错误,我不知道是否需要配置参数接口或实现这些接口的类
parameters:
user.repository.class: App\UserBundle\Repository\UserRepository
user_repository.factory_argument: App\UserBundle\Entity\Usuario
user.service.class: App\UserBundle\Services\UserServiceImpl
user.service.argument: App\UserBundle\Repository\UserRepository
services:
app_user.registration.form.type:
class: App\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: app_user_registration }
login_listener:
class: 'App\UserBundle\Listener\LoginListener'
arguments: ['@security.context', '@doctrine', '@service_container']
tags:
- { name: 'kernel.event_listener', event: 'security.interactive_login' }
app.user.repository:
public: true
class: "%user.repository.class%"
factory_service: doctrine.orm.entity_manager
factory_method: getRepository
arguments: [%user_repository.factory_argument%] #, %user_repository.factory_argument2%]
user.service:
class: "%user.service.class%"
arguments: [%user.service.argument%]
这些是我的课程:
UserRepositoryInterface:
<?php
namespace App\UserBundle\Repository;
use Doctrine\Common\Persistence\ObjectRepository;
interface UserRepositoryInterface extends ObjectRepository
{
public function fetch($id_or_email);
}
UserRepository:
<?php
namespace App\UserBundle\Repository;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
implements UserRepositoryInterface
{
public function fetch($id_or_email)
{
if (!$id_or_email) return;
$user = $this->find($id_or_email);
return $user;
}
}
UserService(界面)
<?php
namespace App\UserBundle\Services;
use Guzzle\Service\ClientInterface;
interface UserService
{
public function getUser($id_or_email);
}
UserServiceImpl:
<?php
namespace App\UserBundle\Services;
use Guzzle\Service\ClientInterface;
use Doctrine\ORM\EntityRepository;
use App\UserBundle\Repository;
class UserServiceImpl implements UserService {
protected $userRepository;
public function __construct(UserRepositoryInterface $fooRepository) //
{
$this->userRepository = $fooRepository;
}
public function getUser($em, $id_or_email=null)
{
error_log("#### getUSer {$id_or_email} ####");
if (!$id_or_email) return;
return $this->userRepository->fetch($id_or_email);
}
}
答案 0 :(得分:0)
正如我在评论中提到的,你需要将第二个参数放到工厂。这是一个有效的例子:
cerad_game__entity_manager__doctrine:
alias: doctrine.orm.games_entity_manager
cerad_game__team_repository__doctrine:
class: Cerad\Bundle\GameBundle\Doctrine\EntityRepository\TeamRepository
factory_service: 'cerad_game__entity_manager__doctrine'
factory_method: 'getRepository'
arguments:
- 'Cerad\Bundle\GameBundle\Doctrine\Entity\Team'
您的服务应如下所示:
app.user.repository:
class: App\UserBundle\Repository\UserRepository
factory_service: doctrine.orm.entity_manager
factory_method: getRepository
arguments:
- App\UserBundle\Usuario