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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:21:35+00:00 2026-05-24T17:21:35+00:00

I have an article-category relationship and I’d like to update the relationship when necessary.

  • 0

I have an article-category relationship and I’d like to update the relationship when necessary. That means, adding or removing the desired categories from an article. I use a php (ZF) setup with Doctrine 1.2. In YAML, the config looks (simplified) like this:

Article:
  columns:
    id: bigint(10)

Category:
  columns:
    id: bigint (10)
  relations:
    articles:
      foreignAlias: categories
      class: Article
      refClass: CategoryArticle

CategoryArticle:
  columns:
    category_id: bigint (10)
    article_id: bigint (10)
  relations:
    category:
      class: Category
      foreignAlias: categoryArticle
    article:
      class: Article
      foreignAlias: categoryArticle

I have a persisted $article where all old categories are available. With a POST request I get a list of category ids which should be the new ones. I have this so far:

$oldCategories = array();
foreach ($article->categories as $cat) {
    $oldCategories[] = $cat->id;
}
$newCategories = $form->getValue('categories');

$diffRemove = array_diff($oldCategories, $newCategories);
$diffAdd    = array_diff($newCategories, $oldCategories);
foreach ($diffRemove as $id) {
    // Remove all the categories $id from article [1]
}
foreach ($diffAdd as $id) {
    // Add all the categories $id to article [2]
}

My question is about [1] and [2]. What is the best performance to add and remove a many:many relationship?

  • 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-24T17:21:36+00:00Added an answer on May 24, 2026 at 5:21 pm

    Deletion

    The most efficient way of deleting en masse in SQL is using single sql statement with some conditions set, something like

    delete from Products where id>3
    

    It make use of indexes, partitioning, etc.

    There is a way to achieve that value of performance using Doctrine 1.2 – using DQL DELETE statements. As shown in documentation, the following query transforms into the above SQL:

    $q = Doctrine_Query::create()
        ->delete('Products p')
        ->where('p.id > 3');
    

    In your case, you could optimize deletion with something like

    $q = Doctrine_Query::create()
        ->delete('CategoryArticle ca')
        ->where('ca.article_id=?', $article_id)
        ->andWhereIn('ca.category_id', $diffRemove);
    

    This code should generate something like:

    delete from CategoryArticle 
    where article_id = $article_id and category_id in ($diffremove)
    

    Insertion

    With insertion you could use CategoryArticle entity. The most effective way to do that with Doctrine is to build up a collection of Entities and than save that collecion:

    $collection = new Doctrine_Collection('CategoryArticle');
    foreach ($diffAdd as $id){
        $ca = new CategoryArticle();
        $ca->category = $id;
        $ca->article = $article_id;
        $collection[] = $ca;
    }
    
    $collection->save();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have 2 entities: Article Category And I have a table that relates
I have a Category class which looks like this: @Entity @Table(name = categories) public
Each Article can have unlimited categories. Categories are saved in the database like this
I have a form for submitting an article. The form has a category multi-select
I have a large xml file (>30mb) that has paths such as yoursite.com/category/section/3389121/title-article I
I have model Article it has field title with some text that may contain
Say that I have an article with multiple pages. Each page has a short
I have read this article from High Scalability about Stack Overflow and other large
Let's say I have two objects: Articles and Categories with a many-many relationship between
I have 2 entities: article category then a table: articles_categories - articleID - categoryID

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.