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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:24:14+00:00 2026-06-05T01:24:14+00:00

In a basic blog application, I have an articles module which allows me to

  • 0

In a basic blog application, I have an articles module which allows me to select the related categories.
There are 3 tables; articles, article_categories, categories. 2 models, Model_Article and Model_Category extending ORM with has-many ‘through’ relationship assigned accordingly. During ‘Edit Article’ form submission, what is the proper way (or better way) to:

  • Retrieve all available category ids in array format to populate Form::select()
  • Retrieve assigned category ids from pivot table, in array format for selected categories
  • Update pivot table upon successful form submission, where some pivot data already exist. Preferably checking existing data before add/remove the relevant ones (instead of deleting all existing pivot data then adding all submitted category ids).

Basically I’m hoping there’s a way to get the pivot data arrays without looping through the ORM objects.

Not sure if what I’m asking for can only be done via database prepared statements/query builder. If so, is it advisable for me to have my own methods (using prepared statements/query builder) added to my Model_Article extend ORM?

The current method I’m using are:

Get all category ids

    $categories_orm = ORM::factory('article_category')->find_all();
    foreach($categories_orm as $category_orm) {
        $categories[$category_orm->id] = $category_orm->name;
    }

Get selected category ids

    $categories_current = $article->article_categories->find_all();
    foreach($categories_current as $category){
        $categories_selected = $category->id;
    }

Update category ids

    $categories_orm = $article->article_categories->find_all();
    $categories_existing = array();
    foreach($categories_orm as $category){
        $categories_existing[] = $category->id;
    }
    $categories_selected = $this->request->post('category');

    $categories_to_remove = array_diff($categories_existing,$categories_selected);
    $categories_to_add = array_diff($categories_selected,$categories_existing);
    if(!empty($categories_to_remove)) {
        $article->remove('article_categories', $categories_to_remove);
    }
    if(!empty($categories_to_add)) {
        $article->add('article_categories', $categories_to_add);
    }
  • 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-06-05T01:24:15+00:00Added an answer on June 5, 2026 at 1:24 am

    You should probably rename your relationship table to articles_categories (both plural) to follow Kohana conventions, and categories is a better alias for the relationship.

    The category model should be used to retrieve all category ID’s. Then you loop through it the same as your example to populate an array.

    Get all categories.

    $categories_orm = ORM::factory('category')->find_all();
    foreach ($categories_orm as $category)
    {
        $categories[$categories_orm->id] = $categories_orm->name;   
    }
    

    Make sure your select element gives post values in an array by naming it like this:

    name="categories[]"
    

    Update relationships.

    $article = ORM::factory('article', $post['article_id']);
    
    // do stuff (validate, update the article etc.)
    
    // remove all relationships
    $article->remove('categories');
    
    // add new relationships
    $article->add('categories', $post['categories']);
    

    I would probably use hidden form fields to check against to first verify if the categories relationship needs to be updated. I’d imagine the categories for an article rarely ever change.

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

Sidebar

Related Questions

I have a line of code in a basic Rails blog application I did
I am building a basic blog module that allows the user to rate the
I have build a basic blog application, where an admin can write the article
Basic question! I have 2 tables PRODUCE +-----+--------------+ | id | fruit_name | +--------------------+
I am building a Django application that is a pretty basic blog, so far
I'd like to implement ASP.Net MVC application which could have controllers, views and content
I have an idea for a large web application, which in my mind would
I am reading the source code of the Django application blog at git://github.com/nathanborror/django-basic-apps.git .
I need to have HTTP server embedded in my .NET application. Simply, there will
I am new to rails and I am creating a basic blog application. When

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.