Doctrine orm 第2条重复检测

Doctrine orm 第2条重复检测,doctrine-orm,duplicates,detection,Doctrine Orm,Duplicates,Detection,我正在尝试创建脚本,将关键字从图像导入数据库。关键字表中的关键字应该是唯一的,但许多图像可以共享相同的关键字。所以我写了两个简单的模型: 图像: class Image { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue */ private $id; /** @ORM\Column(unique=true, nullable=false) *

我正在尝试创建脚本,将关键字从图像导入数据库。关键字表中的关键字应该是唯一的,但许多图像可以共享相同的关键字。所以我写了两个简单的模型:

图像:

class Image
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
     private $id;

     /** @ORM\Column(unique=true, nullable=false) */
     private $filename;

     /**
     * @ORM\ManyToMany(targetEntity="Keyword", inversedBy="images", cascade={"persist"})
     * @ORM\JoinTable(name="image_keyword_binder")
     */
     private $keywords;

     public function __construct()
     {
        $this->keywords = new \Doctrine\Common\Collections\ArrayCollection();
     }
}
class Keyword
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    private $id;

    /**
     * @ORM\Column(type="string", nullable=false, unique=true)
     */
    private $text;

    /**
     * @ORM\ManyToMany(targetEntity="Image", mappedBy="keywords")
     */
    private $images;

    public function __construct()
    {
        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
    }
}
关键字:

class Image
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
     private $id;

     /** @ORM\Column(unique=true, nullable=false) */
     private $filename;

     /**
     * @ORM\ManyToMany(targetEntity="Keyword", inversedBy="images", cascade={"persist"})
     * @ORM\JoinTable(name="image_keyword_binder")
     */
     private $keywords;

     public function __construct()
     {
        $this->keywords = new \Doctrine\Common\Collections\ArrayCollection();
     }
}
class Keyword
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    private $id;

    /**
     * @ORM\Column(type="string", nullable=false, unique=true)
     */
    private $text;

    /**
     * @ORM\ManyToMany(targetEntity="Image", mappedBy="keywords")
     */
    private $images;

    public function __construct()
    {
        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
    }
}
现在,在导入过程中,对于每个我想添加到DB的图像,只有新的关键字,如果图像是用现有关键字描述的,我想添加对这个现有关键字的引用。我希望实现如下代码:

$images[] = array('filename'=>'image1.jpg', 'keywords'=>array('blue', 'green', 'yellow'));
$images[] = array('filename'=>'image2.jpg', 'keywords'=>array('pink', 'green', 'yellow'));
$images[] = array('filename'=>'image2.jpg', 'keywords'=>array('black', 'green', 'red'));

foreach($images as $img)
{
$image = new \FL\Entity\Image();
$image->setFilename($image['filename']);
$image->setKeywords($image['keywords']);

// em is Entity Manager Instance
$em->persist($image);
$em->flush();
}
另一件事要知道的是,我不想在每个循环中都刷新。我将使用这里描述的东西:批量插入部分


这一切都可能吗?条令能否确定某个关键字是否存在,并自动向其添加引用?在每个循环中从DB加载所有现有关键字并将它们与从映像加载的新关键字进行比较并不是一个解决方案。

目前Doctrine2不支持这一点。您必须通过以下方式使用存储库:

$image = $repository->findOneBy(array('filename' => $checkedFileName));
if ($image) {
    // use reference
} else {
    // create new entity instance
}
如果愿意,也可以使用DQL,但这是最快的方法。 所以是的,你会有很多开销。 您可以做的是建立大量尚未持久化的实体,在(:fileNames)条件中使用
,从而将查询数量减少到每个批处理块一个。

无论如何,检查仍由您决定。

到目前为止,我正在以您描述的方式导入照片。我问了一个问题,因为我在寻找更优化的方法。但我知道,目前我无法做得更好,这也是重要的信息。谢谢