class DefaultController extends Controller
{
public function indexAction()
{
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', API);
$data = json_decode($response->getBody()->getContents(), true);
$request = new Request();
foreach ($data as $data) {
$request->setName($data['name']);
}
$em = $this->getDoctrine()->getManager();
$em->persist($request);
$em->flush();
return new Response('Saved new product with id '.$request->getId());
return $this->render('ApiBundle:Default:index.html.twig');
}
由于某种原因,循环不起作用。是否有理由说这个循环不起作用并且有任何解决方法?
答案 0 :(得分:1)
不确定,但在你的for循环中,你使用'$ data'两次。相反应该(使用$ d代替):
foreach ($data as $d) {
$request->setName($d['name']);
}
此外,您可以尝试转储$ data以查看它包含的内容。