【问题标题】:Symfony Call to a member function setFirstname() on a non-objectSymfony 在非对象上调用成员函数 setFirstname()
【发布时间】:2015-04-15 12:52:33
【问题描述】:

我正在尝试使用服务在表中创建行。但它说在非对象上调用成员函数 setFirstname(),这是行: $this->idm->setPersno($science->getPersno());

我在谷歌上搜索了几个小时,但找不到该问题的解决方案。有人可以帮帮我吗?

$scienceList = $this->em->getRepository('MyBundle:MyTable1')->findAll();
            foreach ($scienceList as $science) 
            {
                $idm = new Users();
                $this->idm = $this->em->getRepository('MyBundle:MyOtherTable')->find($science->getPersno());
                if($this->idm == null)
                {
                    $this->idm->setPersno($science->getPersno());
                    $this->idm->setFirstname($science->getFirstname());
                    $this->idm->setLastname($science->getLastname());
                    $this->em->persist($idm);

                }
            }
            $this->em->flush();

【问题讨论】:

    标签: symfony service doctrine entity


    【解决方案1】:
               $this->idm = $this->em->getRepository('MyBundle:MyOtherTable')->find($science->getPersno());
                if($this->idm == null)
                {
                    $this->idm = new Users();
                    $this->idm->setPersno($science->getPersno());
                    $this->idm->setFirstname($science->getFirstname());
                    $this->idm->setLastname($science->getLastname());
                    $this->em->persist($this->idm);
    
                }
    

    【讨论】:

    • 不确定你的代码是如何工作的,问题是你用 $this->idm = $this->em->getRepository... 检查它是否为 null 和然后尝试将 setter 设置为 null 变量,所以我刚刚为 $this->idm 创建了新对象以在该对象上使用 setter
    • 我将 $this->idm 更改为 $idm 并让它按照您的编码方式运行,因为它工作得很好。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2012-04-20
    • 2015-05-01
    • 2014-08-07
    相关资源
    最近更新 更多