I just override the UserManager.php
of the FOSUser Bundle of Symfony2, most particularly the updateUser function (I have the last version of the bundle and last version of the framework).
Here is the function (with the debug parts) :
<?php
/**
* Updates a user.
*
* @param UserInterface $user
* @param Boolean $andFlush Whether to flush the changes (default true)
*/
public function updateUser(UserInterface $user, $andFlush = true, $form = false)
{
$this->updateCanonicalFields($user);
$this->updatePassword($user);
if ($form !== false) {
$em = $this->objectManager;
$user->setStatus($em->getRepository('BirdOfficeBundle:Status')->find(1));
$language = $em->getRepository('BirdOfficeBundle:Language')->getLanguage(1);
echo '<pre>';
var_dump($language);die();
//$user->setLanguage($em->getRepository('BirdOfficeBundle:Language')->find(1));
//$user->setType($em->getRepository('BirdOfficeBundle:Type')->find(1));
echo '<pre>';var_dump($user);die();
}
$this->objectManager->persist($user);
if ($andFlush) {
$this->objectManager->flush();
}
}
?>
What I want to do with this function is to call it from my RegistrationController.php
(registerAction method) after the submit of my register form.
The problem is the following :
When I set my status, it's working very well. When I try to use the find method from other repositories (like Langue, Type, or whatever), my web browser crash.
I tried to use it in another controller, for test, and it works very well.
So, what can be the problem ?
By the way, I tried to use findAll or personal queries, but it crashes too.
I checked my dev.log
file, and there is no event.ERROR
in it.
I finally find a solution. I removed the find()
calls for my own functions (getStatus
, getLanguage
, getType
) and I put a try/catch call inside.
Now, it works really good !
Here is the code for Acme/AcmeBundle/Repository/LanguageRepository.php
<?php
namespace Acme\AcmeBundle\Repository;
use Doctrine\ORM\EntityRepository;
class LanguageRepository extends EntityRepository
{
public function getLanguage($id) {
$db = $this
->createQueryBuilder('l')
->andWhere('l.id = :id')
->setParameter('id', $id)
;
try {
$language = $db->getQuery()->getSingleResult();
} catch (\Doctrine\Orm\NoResultException $e) {
$language = null;
}
return $language;
}
}
And the code of my UserManager.php
method :
<?php
$language = $em->getRepository('AcmeBundle:Language')->getLanguage(1);
if (is_null($language)) {
$language = 1;
}
$user->setLanguage($language);
After that, I launch a php app/console cache:clear
Thank you very much ;)
Gabriel
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments