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

  • Home
  • SEARCH
  • 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 6924693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:41:42+00:00 2026-05-27T10:41:42+00:00

CakePHP does not have a built in mechanism for handling reordering of records, as

  • 0

CakePHP does not have a built in mechanism for handling reordering of records, as far as I know. So, I’m using the Ordered Behavior that, as near as I can tell, is the defacto behavior for reordering in CakePHP. So far it’s working well as I add it to various models, however, I’ve got a situation I’m not sure how to deal with.

My model hierarchy is as follows.

Section > Heading > Category > Item

However, Items can be attached directly to sections:

Section > Item

The Item model’s table has both category_id and section_id defined, though only one is actually used for any given record.

The ordered behavior has two parameters set when you set the model’s $actsAs. Here’s the one for my Heading model:

var $actsAs = array('Ordered' => array('field' => 'order','foreign_key' => 'section_id'));

How should I define the $actsAs member for the Item model where it has two foreign keys section_id and category_id to ensure that the ordering/sequence is properly maintained?

  • 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-27T10:41:42+00:00Added an answer on May 27, 2026 at 10:41 am

    I haven’t tested this extensively, but with my first try this appears to be a workaround that will let me dynamically manage the ordering. Inside of my ItemsController I’ve got the following actions:

        function moveup($id = null) {
            if (!$id) {
                $this->Session->setFlash(__('Invalid id for Item', true));
            } else {
                $item = $this->Item->read(null, $id);
    
                if ($item['Item']['section_id'] != 0) {
                    $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'section_id'));
                } else {
                    $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'category_id'));
                }
    
                if ($this->Item->moveup($id)) {
                    $this->Session->setFlash(__('Item moved up', true));
                } else {
                    $this->Session->setFlash(__('Item move failed', true));
                }
            }
    
            $this->redirect($this->referer());
        }
    
        function movedown($id = null) {
            if (!$id) {
                $this->Session->setFlash(__('Invalid id for Item', true));
            } else {
                $item = $this->Item->read(null, $id);
    
                if ($item['Item']['section_id'] != 0) {
                    $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'section_id'));
                } else {
                    $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'category_id'));
                }
    
                if ($this->Item->movedown($id)) {
                    $this->Session->setFlash(__('Item moved down', true));
                } else {
                    $this->Session->setFlash(__('Item move failed', true));
                }
            }
    
            $this->redirect($this->referer());
        }
    

    My model doesn’t set the ordered behavior (and thus the foreign_key) via the $actsAs member. Instead, prior to any order manipulation, I determine the parent item type and attach the behavior, with appropriate foreign_key at run time.

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

Sidebar

Related Questions

I am using SendGrid and PHP (CakePHP). My server does not have SMTP enabled.
How does CakePHP deal with tables that don't have an id column? HABTM is
I have built a cakephp app that needs the user to login or register(for
I have a CakePHP ticketing app where I am using Revision Behavior to keep
For CakePHP errors I know that there exists CakeError and AppError solutions. But I
I am using CakePHP. I created an external class which is not a model
I am using Cakephp as my framework. I have a problem in uploading my
I have a problem when using CakePHP HABTM. I have the following models. class
I'm using CakePHP v2.2.1 stable. I have a UsersController with the action add() .
I have some component code that I'd like to in a CakePHP Task, but

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.