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条记录。请给我这个解决方案。
实体类是国家唯一的......我已经验证.. –
我怎样才能在Symfony2中实现这一@nifr '的foreach($ codesArray为$细节) \t { \t \t $ detailArray = explode(“;”,$ detail); \t \t $ query =“update country set code =”。$ detailArray [1]。“where name =”。$ detailArray [0]; \t}' –
答案更新... – nifr