duannaiying9662 2013-05-27 09:28
浏览 36
已采纳

在symfony2中调用非对象错误的成员函数setCode()

I have a cotroller action like

public function updateAction()
    {
        $em = $this->getDoctrine()->getManager();
        $codesArray=array('AFGHANISTAN;AF','ÅLAND ISLANDS;AX','ALBANIA;AL');
        foreach ($codesArray as $detail)
        {
            $detailArray=explode(";",$detail);
            $country=$em->getRepository('TestBundle:Countries')
                          ->findOneBy(array('name'=>$detailArray[0]));
            $country->setCode($detailArray[1]);
        }
        $em->flush();
    }

it shows error

FatalErrorException: Error: Call to a member function setCode() on a non-object

but when i replace

$country=$em->getRepository('TestBundle:Countries')
                          ->findOneBy(array('name'=>$detailArray[0]));

with

$country=$em->getRepository('TestBundle:Countries')
                              ->find('1');

means it will update 1st record. please give me the solution for this.

  • 写回答

1条回答 默认 最新

  • duanfei8399 2013-05-27 10:15
    关注

    Repository ( and Entity ) names are normally singular.

    If your entity class is Country you get your Repository with:

     $country=$em->getRepository('TestBundle:Country')-> ...
    

    I don't know what you're trying to do but you can get those Country codes from the symfony2 Intl component ...

    use Symfony\Component\Intl\Intl;
    
    \Locale::setDefault('en');
    
    $countries = Intl::getRegionBundle()->getCountryNames();
    // => array('AF' => 'Afghanistan', ...)
    
    $country = Intl::getRegionBundle()->getCountryName('GB');
    // => 'United Kingdom'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题