P
P
Plamer2016-12-23 13:27:35
PHP
Plamer, 2016-12-23 13:27:35

Doctrine 2, why are records duplicated on large insert?

Hello, I don't understand what is the problem. When I try to insert 3 - 10 tons of rows through Doctrine. the base comes exactly twice as much. That is, the algorithm is somehow strange, I insert 5 thousand rows into the table (A), then I start inserting 3 thousand rows into the table (B) and just these 3 thousand, for some reason 6 thousand enters the database. At the same time, if I start again to run the script to insert into the table (B) 3 thousand, then the second time it will go without multiplying by 2. I checked the cycles, everything is fine, this is Doctrine doing something inside itself. In doing so, I do $em->flush, $em-clear(), every 200 entries. Tried clearing cache requests\result through console, still doesn't work. Help someone please, I’ve been sitting for more than an hour and I can’t figure it out, while there is no information about this.
Function:

public function test(){
        $reader = ReaderFactory::create(Type::XLSX);
        $file = base_path() . '/excel/cars.xlsx';
        $reader->open($file);

        foreach ($reader->getSheetIterator() as $sheet) {
            foreach ($sheet->getRowIterator() as $key => $row) {
                $test = new \Entities\Test();
                $test
                    ->setCod('CC')
                    ->setName($row[0])
                    ->setImg('')
                    ->setLang(1);

                EntityManager::persist($test);
                if($key % 200 == 0){
                    EntityManager::flush();
                    EntityManager::clear();
                }
            }
        }
        EntityManager::flush();
        EntityManager::clear();
        $reader->close();
    }

Essence:
<?php
namespace Entities;
use Doctrine\ORM\Mapping as ORM;

/**
 * Rating
 *
 * @ORM\Table(name="test")
 * @ORM\Entity(repositoryClass="\Entities\repositories\TestRepository")
 */
class Test
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="cod", type="string", length=3, nullable=false)
     */
    private $cod;

    /**
     * @var string
     *
     * @ORM\Column(name="img", type="string", length=50, nullable=false)
     */
    private $img;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100, nullable=false)
     */
    private $name;

    /**
     * @var integer
     *
     * @ORM\Column(name="lang", type="integer", nullable=false)
     */
    private $lang;

    public function getId()
    {
        return $this->id;
    }

    public function setCod($cod)
    {
        $this->cod = $cod;
        return $this;
    }

    public function getCod()
    {
        return $this->cod;
    }

    public function setImg($img)
    {
        $this->img = $img;
        return $this;
    }

    public function getImg()
    {
        return $this->img;
    }

    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setLang($lang)
    {
        $this->lang = $lang;
        return $this;
    }

    public function getLang()
    {
        return $this->lang;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2016-12-23
@plamer

running through a browser? Maybe there is no favicon, the browser asks your code for it and the code runs again? And the fact that the first time passes the rules, and the second does not, then the session may be blocked

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question