我有两个表:文章和电子邮件.
电子邮件表将包含与特定文章相关的电子邮件,例如:
原文链接:https://www.f2er.com/php/132309.html电子邮件表将包含与特定文章相关的电子邮件,例如:
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 { .... /** * @ORM\OneToMany(targetEntity="\Acme\DemoBundle\Entity\Emails",mappedBy="articles") */ private $emails_rel; ... } class Emails { /** * @ORM\ManyToOne(targetEntity="\Acme\DemoBundle\Entity\Articles",inversedBy="emails_rel",cascade={"all"}) * @ORM\JoinColumn(name="article_id",referencedColumnName="id") **/ private $articles; }
在我的控制器中,我正在做一些测试,我将坚持或不坚持某些实体.
最后,我正在做同花
$em->flush();
而奇怪的行为是,在我的电子邮件表中,一旦我进行刷新,数据就会被复制.使用时测试实体管理器
$em->getUnitOfWork()->getScheduledEntityInsertions()
我得到一个空数组.
知道为什么吗?非常感谢你.
编辑:
这是测试:
$articl = new Articles(); $articl = $em->createQuery("SELECT p FROM Acme\DemoBundle\Entity\Articles 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 "\n\n---------- $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"\n\n$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 " ==> ok\n"; } if (($i % 3) == 0) { $em->flush(); $em->clear(); } } $em->flush(); $em->clear(); } return new Response ( "\n\nFinished!!\n" );