我有一个控制器动作,比如
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();
}
它显示错误
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');
意味着它将更新第一条记录。请给我解决方案。