Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7563485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:40:42+00:00 2026-05-30T13:40:42+00:00

I’m trying to create script, that will import keywords from images to database. Keywords

  • 0

I’m trying to create script, that will import keywords from images to database. Keywords in keyword table should be unique, but many images can share the same keyword. So I have wrote those two simple models:

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();
    }
}

Now, during import, for every image i want to add to DB only new keywords, and if image is described with existing keyword i want add reference to this existing one. I want to achieve code like this:

$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();
}

Another thing to know is that I dont want to make a flush in every loop. I will be using something like described here: http://readthedocs.org/docs/doctrine-orm/en/latest/reference/batch-processing.html in Bulk Inserts part.

Is this all possible? Can Doctrine determine if certain keyword exists and automaticly add only refrence to it? Loading all existing keywords from DB in every loop and comparing them with new loaded from image isn’t a solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:40:43+00:00Added an answer on May 30, 2026 at 1:40 pm

    This is not supported by Doctrine2 at the moment. You will have to use your repository through

    $image = $repository->findOneBy(array('filename' => $checkedFileName));
    if ($image) {
        // use reference
    } else {
        // create new entity instance
    }
    

    You can also use DQL if you prefer, but this is the fastest way.
    So yes, you will have a lot of overhead.
    What you can do to reduce it is building up large chunks of not yet persisted entities, use a IN(:fileNames) condition and so reduce the number of queries to one per batch chunk.
    Anyway, the check is still up to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.