不确定我做错了什么,这个问题的类似答案也无济于事。它在表单提交后发生。
错误讯息:
FatalErrorException:错误:在C:\ xampp \ htdocs \ EliteFifa2 \ src \ EliteFifa \ Bundle \ Manager \ BaseManager.php第18行中的非对象上调用成员函数persist()
UserController中:
public function selectTeamAction(Request $request)
{
$userManager = $this->get('elite_fifa.user_manager');
$loggedInUser = $userManager->getLoggedInUser();
$teamManager = $this->get('elite_fifa.team_manager');
$teams = $teamManager->getTeamsWithoutAManager();
$teamChoices = array();
foreach ($teams as $team) {
$teamChoices[$team->getId()] = $team->getName();
}
$form = $this->createFormBuilder()
->add('teams', 'choice', array(
'choices' => $teamChoices,
))
->add('save', 'submit')
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$selectedTeam = $teamManager->getTeamById($data['teams']);
$loggedInUser->setTeam($selectedTeam);
$userManager->persist($loggedInUser);
return $this->redirect($this->generateUrl('task_success'));
}
return $this->render('EliteFifaBundle:User:selectTeam.html.twig', array(
'teams' => $teams,
'form' => $form->createView()
));
}
的UserManager:
class UserManager extends BaseManager
{
private $userRepository;
private $loggedInUser;
public function __construct(UserRepository $userRepository, User $loggedInUser)
{
$this->userRepository = $userRepository;
$this->loggedInUser = $loggedInUser;
}
public function createUser()
{
return new User();
}
public function getLoggedInUser()
{
return $this->loggedInUser;
}
public function getUserByUsername($username)
{
return $this->userRepository->findOneByUsername($username);
}
}
BaseManager:
class BaseManager
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function persist($entity)
{
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
public function remove($entity)
{
$this->entityManager->remove($entity);
$this->entityManager->flush();
}
}
Services.yml:
parameters:
user_repository.class: EliteFifa\Bundle\Repository\UserRepository
user_repository.factory_argument: 'EliteFifaBundle:User'
role_repository.class: EliteFifa\Bundle\Repository\RoleRepository
role_repository.factory_argument: 'EliteFifaBundle:Role'
team_repository.class: EliteFifa\Bundle\Repository\TeamRepository
team_repository.factory_argument: 'EliteFifaBundle:Team'
match_repository.class: EliteFifa\Bundle\Repository\MatchRepository
match_repository.factory_argument: 'EliteFifaBundle:Match'
user_manager.class: EliteFifa\Bundle\Manager\UserManager
base_manager.class: EliteFifa\Bundle\Manager\BaseManager
role_manager.class: EliteFifa\Bundle\Manager\RoleManager
team_manager.class: EliteFifa\Bundle\Manager\TeamManager
match_manager.class: EliteFifa\Bundle\Manager\MatchManager
services:
elite_fifa.base_manager:
class: %base_manager.class%
arguments: [@doctrine.orm.entity_manager]
elite_fifa.user_manager:
class: %user_manager.class%
arguments:
- @elite_fifa.user_repository
- @=service('security.context').getToken().getUser()
elite_fifa.role_manager:
class: %role_manager.class%
arguments:
- @elite_fifa.role_repository
elite_fifa.team_manager:
class: %team_manager.class%
arguments:
- @elite_fifa.team_repository
elite_fifa.match_manager:
class: %match_manager.class%
arguments:
- @elite_fifa.match_repository
elite_fifa.user_repository:
class: %user_repository.class%
factory_service: doctrine.orm.entity_manager
factory_method: getRepository
arguments:
- %user_repository.factory_argument%
elite_fifa.role_repository:
class: %role_repository.class%
factory_service: doctrine.orm.entity_manager
factory_method: getRepository
arguments:
- %role_repository.factory_argument%
elite_fifa.team_repository:
class: %team_repository.class%
factory_service: doctrine.orm.entity_manager
factory_method: getRepository
arguments:
- %team_repository.factory_argument%
elite_fifa.match_repository:
class: %match_repository.class%
factory_service: doctrine.orm.entity_manager
factory_method: getRepository
arguments:
- %match_repository.factory_argument%