Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 我有两个实体和两个存储库。如何在实体中保存数据?_Symfony_Doctrine Orm - Fatal编程技术网

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();

这是医生,读起来更好。。。