Symfony 我有两个实体和两个存储库。如何在实体中保存数据?
名为Symfony 我有两个实体和两个存储库。如何在实体中保存数据?,symfony,doctrine-orm,Symfony,Doctrine Orm,名为国家和地区的实体 名为CountryRepository和RegionRepository的存储库 如何在区域实体中存储数据 $em = $this->container->get('doctrine')->getEntityManager(); $countryId=$em->getRepository('LocationBundle:Country')->find(1)); $region=new Region(); //How to create Reg
国家
和地区
的实体
名为CountryRepository
和RegionRepository
的存储库
如何在区域
实体中存储数据
$em = $this->container->get('doctrine')->getEntityManager();
$countryId=$em->getRepository('LocationBundle:Country')->find(1));
$region=new Region(); //How to create Region Ojbect
$region->setCountryId($countryId);
$region->setName('abc');
$region->save();
首先,您应该修改控制器操作的第一行,如下所示:
$em = $this->getDoctrine()->getManager();
其次,您必须在其实体中实现Region的构造函数,并且在Controller中必须正确地传递构造函数的参数
最后,要将新区域保存到数据库中,您必须使用以下语句:
$em->persist($region);
$em->flush();
这是医生,读起来更好。。。