有人能用这段代码告诉我原因:
<?php
public function continentAndCountriesAction()
{
$manager = $this->getDoctrine()->getManager();
$repository = $manager->getRepository('FkgDemoBundle:Continent');
$europeEntity = $repository->findOneByName('Europe');
if (!$europeEntity) {
$europeEntity = new Continent();
$europeEntity->setName('Europe');
$manager->persist($europeEntity);
}
$countries = array('Poland', 'Germany', 'Czech Republic', 'Italy', 'France');
foreach ($countries as $country) {
$country = new Country();
$country->setName($country);
$country->setContinent($europeEntity);
$manager->persist($country);
}
$manager->flush();
$entities = $repository->findAll();
return $this->render('FkgDemoBundle:Default:continent.html.twig', compact('entities'));
}
我在视图中只显示“欧洲”(大陆),但如果我将保存实体删除到数据库,如下所示:
<?php
public function continentAndCountriesAction()
{
$manager = $this->getDoctrine()->getManager();
$repository = $manager->getRepository('FkgDemoBundle:Continent');
$europeEntity = $repository->findOneByName('Europe');
if (!$europeEntity) {
throw $this->createNotFoundException('Continent not found.');
$entities = $repository->findAll();
return $this->render('FkgDemoBundle:Default:continent.html.twig', compact('entities'));
}
一切都按照我的预期加载 - 欧洲与国家?
Profiler在插入http://screencast.com/t/MZuTdUHJIJc并拥有最后查询(findAll())时为我提供了12个查询http://screencast.com/t/dfDSprfjV9y 但如果我不保存实体,则findAll()结果不同:http://screencast.com/t/KoCZcsp00th。
使用Doctrine2加载实体时是否“神奇”? Symfony 2.3.7 学说2.3.4