2013-05-27 36 views
0

我有这样呼叫一个成员函数setCode()在非对象错误在Symfony2的

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(); 
    } 

一个cotroller动作它显示错误

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

但是当我更换

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

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

表示它将更新第1条记录。请给我这个解决方案。

回答

0

存储库(和实体)名称通常是单数。

如果你的实体类是国家,你让你的存储库:

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

我不知道你想做什么,但你可以从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' 
+0

实体类是国家唯一的......我已经验证.. –

+0

我怎样才能在Symfony2中实现这一@nifr '的foreach($ codesArray为$细节) \t { \t \t $ detailArray = explode(“;”,$ detail); \t \t $ query =“update country set code =”。$ detailArray [1]。“where name =”。$ detailArray [0]; \t}' –

+0

答案更新... – nifr

相关问题