Php Symfony2和条令:OneToMany关系复制表中的数据
我有两张桌子:文章和电子邮件。 电子邮件表将包含与特定文章相关的电子邮件,例如:Php Symfony2和条令:OneToMany关系复制表中的数据,php,sql,symfony,doctrine-orm,entity,Php,Sql,Symfony,Doctrine Orm,Entity,我有两张桌子:文章和电子邮件。 电子邮件表将包含与特定文章相关的电子邮件,例如: 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 | 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();
奇怪的是,在我的Emails表中,数据一经刷新就被复制了。使用测试实体管理器时
$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" );
如果我阅读正确,那么代码的简化版本如下:
$articles_with_mail_to_match_true = $em->createQuery("SELECT p FROM Acme\DemoBundle\Entity\Articles p WHERE p.bMailToMatch = 1")->getResult();
foreach($articles_with_mail_to_match_true as $i => $article_with_mail_to_match_true)
{
if ( count($article_with_mail_to_match_true->getEmailsArray()) < 1 )
{
$article_copy_object = $em->getRepository('AcmeDemoBundle:Articles')->findOneBy(array( 'id' => ($article_with_mail_to_match_true->getId()) )) ;
$article_copy_object->setBMailToMatch(0);
$em->persist($article_copy_object);
}
if (($i % 3) == 0) {
$em->flush();
}
}
$em->flush();
$articles\u with\u mail\u to\u match\u true=$em->createQuery(“从Acme\DemoBundle\Entity\articles p中选择p,其中p.bMailToMatch=1”)->getResult();
foreach($articles\u with\u mail\u to\u match\u true为$i=>$article\u with\u mail\u to\u match\u true)
{
如果(计数($article_with_mail_to_match_true->getEmailsArray())<1)
{
$article\u copy\u object=$em->getRepository('AcmeDemoBundle:Articles')->findOneBy(数组('id'=>($article\u with_mail\u to_match\u true->getId());
$article\u copy\u object->setBMailToMatch(0);
$em->persist($article\u copy\u object);
}
如果($i%3)==0){
$em->flush();
}
}
$em->flush();
似乎在更改了一个值之后,唯一要持久化的是$article\u copy\u对象。如果是这样的话,那么要么是A.您的实体中有错误,我们在这个代码示例中看不到,要么是B.它发生在其他地方,而不是在这个操作过程中。在我复制它之前,我没有发现您的问题,但现在它是一种明显的问题。 你的问题来自于使用clear方法。 事实上,如果您清除实体管理器,它将忘记它们的存在,并将它们作为新的实体进行保存 如果您有性能问题,您可以限制您处理的项目数量,并递归执行,直到完成为止 我希望这对你有帮助;) 祝你好运
干杯你能把你正在测试的代码放进去吗?@RafaelAdel,我从我的控制器上传了代码如果你删除
$em->clear(),它能工作吗代码>来自任何地方?另外,为什么您不进行一次update
查询,例如updateacme\DemoBundle\Entity\Articles p SET p.bMailToMatch=0,其中p.emails\u rel为空
您可以指定duplicated的含义吗?这个if语句用于什么<代码>如果($i%3)==0{
为什么您只希望每三次迭代刷新和清除一次?这就是我在阅读clear调用时看到的。AFAIK原则在EntityManager中保留对象到数据库记录的映射(因此它不会每次都检查表中的id)。因此,如果您清除em,您拥有的每个对象都将看起来是新的