我正在尝试在我的数据库中创建许多相同的对象,以更准确地插入一些用户。首先,我尝试在每次循环后刷新实体管理器,但它不起作用。经过一些研究,我发现我可以批量进行,所以我尝试每15个循环刷一次,但是我仍然会在30秒后(大约180次查询)超时。对于小到200-250的数字,它们不应该快速,或几乎是即时的吗?我怎样才能让它没有超时并更快地插入它们?
我的功能是向randomuser.me发出请求并检索用户的json。我将该数据检索到一个数组中并使用它来填充我的用户信息。我还有一个$ fields数组,它有某些医学领域(不会在这里发布,因为它只是一个纯文本数组)。它还为用户条目添加了相关的设置条目(包含医生工作的字段和他的图片)。
public function generateAction()
{
$em = $this->getDoctrine()->getManager();
$generated = 0;
$url = "https://randomuser.me/api/?results=250&nat=us";
$str = file_get_contents($url);
$medics = json_decode($str);
$medicsgruop = $this->getDoctrine()->getRepository('MedAppBundle:Group')->findOneBy(array('name' => 'medics'));
if (!$medicsgruop) {
$medicsgruop = new Group('medics');
$medicsgruop->addRole('ROLE_MEDIC');
$em->persist($medicsgruop);
$em->flush();
}
$batchSize = 15;
foreach ($medics->results as $i => $medic) {
if ($this->fields[$i]) {
$field = $this->fields[$i];
} else {
$field = array_rand($this->fields);
}
$fname = $medic->user->name->first;
$lname = $medic->user->name->last;
$image = $medic->user->picture->large;
$email = $medic->user->email;
$user = new User();
//user
$user->setUsername($lname.$fname);
$user->setFirstname($fname);
$user->setLastname($lname);
$user->setEmail($email);
$user->setEnabled(true);
$user->setPlainPassword($fname.$lname);
//group
$user->addGroup($medicsgruop);
$em->persist($user);
//MEDIC SETTINGS
$medsett = new MedicSettings();
$medsett->setField($field);
$medsett->setProfile($image);
$medsett->setMedic($user);
$em->persist($medsett);
if (($i % $batchSize) == 0) {
$em->flush();
$em->clear();
}
}
$em->flush();
$em->clear();
return $this->render(
'@MedApp/Admin/generatemedics.html.twig',
array('generated' => $generated, 'medics' => 'asd')
);
}
使用批处理方法时,大约有40个用户被添加到我的数据库中,但在达到最大执行时间后会停止。我做错了什么?
答案 0 :(得分:0)
使用malcolm的建议来使用symfony中的Client命令。但该命令仍然运行缓慢。每个用户约1秒钟。并且它们每批次一次插入DB 15中。
class GenMedicsCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('doctrine:generate:medics')
->setDescription('Generate medics group, users, group-users, medic settings')
->addArgument(
'number',
InputArgument::REQUIRED,
'How many medics to generate?'
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$number = $input->getArgument('number');
$this->generate($number,$output);
}
protected $fields = array(
'Abdominal Radiology Radiology-Diagnostic',
'Addiction Psychiatry Psychiatry',
'Adolescent Medicine Pediatrics'
);
//generate random medics for the win
public function generate($nr, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$doctrine = $this->getContainer()->get('doctrine');
$generated = 0;
$url = "https://randomuser.me/api/?results=".$nr."&nat=us";
$str = file_get_contents($url);
$medics = json_decode($str);
$medicsgruop = $doctrine->getRepository('MedAppBundle:Group')->findOneBy(array('name' => 'medics'));
if (!$medicsgruop) {
$medicsgruop = new Group('medics');
$medicsgruop->addRole('ROLE_MEDIC');
$em->persist($medicsgruop);
$em->flush();
}
$batchSize = 15;
foreach ($medics->results as $i => $medic) {
// set_time_limit(2);
$randkey = array_rand($this->fields);
$field = $this->fields[$randkey];
$fname = $medic->user->name->first;
$lname = $medic->user->name->last;
$image = $medic->user->picture->large;
$email = $medic->user->email;
$user = new User();
//user
$user->setUsername($lname.$fname);
$user->setFirstname($fname);
$user->setLastname($lname);
$user->setEmail($email);
$user->setEnabled(true);
$user->setPlainPassword($fname.$lname);
//group
$user->addGroup($medicsgruop);
$em->persist($user);
//MEDIC SETTINGS
$medsett = new MedicSettings();
$medsett->setField($field);
$medsett->setProfile($image);
$medsett->setMedic($user);
$em->persist($medsett);
$output->write(array($i+1,'|',$fname,'|',$lname,'|',$field,'|',$email,'|',$image));
$output->writeln('');
if (($i % $batchSize) == 0) {
$em->flush();
// $em->clear();
}
}
$em->flush();
$em->clear();
$output->writeln('done');
}
}