php – Symfony2和Doctrine:OneToMany关系复制表中的数据
发布时间:2020-12-13 16:32:50 所属栏目:PHP教程 来源:网络整理
导读:我有两个表:文章和电子邮件. 电子邮件表将包含与特定文章相关的电子邮件,例如: id | article_id | email1 | 1 | test@etest.com2 | 1 | test2@test.com3 | 2 | test@etest.com4 | 2 | test3@test.com 等等…. article_id与articles表中的id之间存在关系.
我有两个表:文章和电子邮件.
电子邮件表将包含与特定文章相关的电子邮件,例如: id | article_id | email 1 | 1 | test@etest.com 2 | 1 | test2@test.com 3 | 2 | test@etest.com 4 | 2 | test3@test.com 等等…. article_id与articles表中的id之间存在关系. 在我的实体中,我有这个代码: class Articles { .... /** * @ORMOneToMany(targetEntity="AcmeDemoBundleEntityEmails",mappedBy="articles") */ private $emails_rel; ... } class Emails { /** * @ORMManyToOne(targetEntity="AcmeDemoBundleEntityArticles",inversedBy="emails_rel",cascade={"all"}) * @ORMJoinColumn(name="article_id",referencedColumnName="id") **/ private $articles; } 在我的控制器中,我正在做一些测试,我将坚持或不坚持某些实体. $em->flush(); 而奇怪的行为是,在我的电子邮件表中,一旦我进行刷新,数据就会被复制.使用时测试实体管理器 $em->getUnitOfWork()->getScheduledEntityInsertions() 我得到一个空数组. 知道为什么吗?非常感谢你. 编辑: 这是测试: $articl = new Articles(); $articl = $em->createQuery("SELECT p FROM AcmeDemoBundleEntityArticles p WHERE p.bMailToMatch = 1")->getResult(); $nb = count($articl); // BECAUSE I WILL WORK WITH A LOTS OF ENTRIES - PREVENT MEMORY OVERFLOW $em->clear(); if(isset( $articl )) { foreach($articl as $i => $art) { $array_emails = null; $art_emails = $art->getEmailsRel(); foreach ($art_emails as $e) { $array_emails[] = $e->getEmail(); } $art_id = $art->getId (); echo "nn---------- $i ----------n " . $art->getDoi (); // TAKES ARTICLE WITH ZERO EMAIL if (!isset($array_emails) ) { $updated_article = $em->getRepository('AcmeDemoBundle:Articles')->findOneBy(array( 'id' => ($art_id) )) ; // Because of the clearing of the entity manager echo"nn$art_id Article DOI n".$updated_article->getDoi(); echo "n==>Put the BMailToMatch's flag to 0"; $updated_article->setBMailToMatch(0); $em->persist($updated_article); } else { echo " ==> okn"; } if (($i % 3) == 0) { $em->flush(); $em->clear(); } } $em->flush(); $em->clear(); } return new Response ( "nnFinished!!n" );
在我复制它之前我没有发现你的问题,但现在它显而易见了.
您的问题来自于使用clear方法. 实际上,如果你清除你的实体经理,它会忘记它们的存在,并将它们作为新的存在. 如果您遇到性能问题,可以限制处理的项目数量,并在完成之前递归执行. 我希望它能帮到你;) 祝好运 干杯 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |