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 4079224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:46:27+00:00 2026-05-20T17:46:27+00:00

Having a hard time wrapping my head around this one. I have a Page

  • 0

Having a hard time wrapping my head around this one. I have a Page entity and a Keyword entity. I’ve created a member variable called $keywords in my Page entity that has a ManyToMany association that creates a join table called “pages_keywords”. In my application, I want to give the ability to add a keyword to a page. How would I go about adding that record to the database?

I can access the Page entity’s $keywords member variable but I’m not sure where to go from there. I see that the Doctrine implementation of ArrayCollection has an add() method that just takes a value. Can I just pass it the Keyword entity’s id and will that create the record for me?

  • 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-20T17:46:28+00:00Added an answer on May 20, 2026 at 5:46 pm

    Assuming you know the primary key value of the entity you are looking to associate it with (in your example, the $keywordId), the best way to do this is to get a reference to the entity. This gets you a reference without doing a database lookup for the associated entity.

    The solution you posted in your follow-up answer has the disadvantage of requiring an extra trip to the database to retrieve the keyword record. Retrieving it as a reference means that there is no extra database lookup:

    <?php
    
    // assumes $this->em is already defined as the Doctrine2 EntityManager
    $page = $this->em->getRepository('App\Entity\Page')->find($pageId);
    
    // associate $keyword with $page via a reference to the keywordId
    $keyword = $this->em->getReference('App\Entity\Keyword', $keywordId);
    $page->getKeywords()->add($keyword);
    $em->flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having hard time wrapping my head around this. I have an array called
I'm having a hard time wrapping my head around this and need some help
I'm having a very hard time wrapping my head around this problem (it might
I'm having hard time getting my head wrapped around this one - and it
I'm having a hard time wrapping my head around this, and I'm hoping someone
I'm having a hard time wrapping my head around this. I'm loading up an
i am having a hard time wrapping my head around how to do this...
I am still having a hard time wrapping my head around this. I want
I am having a hard time wrapping my head around this problem... I am
I'm having a hard time wrapping my head around this Django query. I could

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.