나는 스크립트에서 이미지를 데이터베이스로 가져 오는 스크립트를 만들려고합니다. 키워드 표의 키워드는 고유해야하지만 많은 이미지가 동일한 키워드를 공유 할 수 있습니다. 그래서 나는 그 두 가지 간단한 모델을 썼다 :교리 2 중복 검색
IMAGE :
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();
}
}
KEYWORD :
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();
}
또 다른 한가지는 모든 루프에서 플러시를하고 싶지 않다는 것입니다. 여기에 설명 된 것과 같은 것을 사용합니다 : http://readthedocs.org/docs/doctrine-orm/en/latest/reference/batch-processing.html in Bulk Inserts part.
이 모든 것이 가능합니까? Doctrine은 특정 키워드가 존재하는지 여부를 판단 할 수 있으며 자동으로 그것에 대한 참조 만 추가 할 수 있습니까? DB의 모든 기존 키워드를 모든 루프에로드하고이를 이미지에서로드 된 새로운 키워드와 비교하는 것은 해결책이 아닙니다.
지금까지 설명한대로 사진을 가져 왔습니다. 내가 더 최적화 된 방법을 찾고 있었기 때문에 나는 질문을했다. 그러나 내가 알지 못하는 순간 더 중요한 정보이기도합니다. 감사. – qchar