dououde4065 2015-01-15 13:49
浏览 47
已采纳

无限循环存储库symfony2 fos用户包

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.

  • 写回答

1条回答 默认 最新

  • dpfz27768 2015-01-15 14:08
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路