Keeping the sort order of items stored in the HABTM association - CakePHP

In the ActiveRecord setting (CakePHP flavored), I have the HasAndBelongsToMany association setting on the video and trash: Bin can store n links to the video, and the video can belong to n Bins.

I need to be able to manually set and save the video display order in a specific bin (so that the client can have their videos in a certain order in the basket). My initial thought is to create a field in Bin that stores an array of video IDs in the order in which they should appear. Is this the most efficient way to handle this?

If so, when I get the result of the HABTM query, then what is the cleanest / most efficient way to reorder the returned query according to a sorted array of identifiers?

The videos associated with the bean are returned as an array:

[Video] => Array
            (
                [0] => Array
                    (
                        [id] => 49b2de95-0338-48b9-bc88-01ba35664358
                        ...

                    )

                [1] => Array
                    (
                        [id] => 49b1ccad-b770-410e-be46-03a035664358
                        ...
            )

Or is there a better way to achieve what I'm trying to do without using the HABTM associations?

Thanks in advance -

+3
source share
3 answers

What to do when the HABTM gets complicated?

HasAndBelongsToMany Cake , . , , 10 . 2 . 2 , 12.

, ( ), HABTM, , .

HasAndBelongsToMany - , hasMany, .

:

Child hasAndBelongsToMany Club

-

Child hasMany Membership
Membership belongsTo Child, 
ClubClub hasMany Membership.

. . , "join" -, .

. , habtm . "", . , , ( ), .

id, bin_id, video_id, weight

bin_id "foreign_key" . .

+6

, , :

. , , id .

public $hasAndBelongsToMany = array(
    'Item' => array(
        'order' => 'ItemFoldersItem.id ASC, Item.name DESC',
    )
);

, ?

+2

" " , , , . , .

! :

$yourData = Set::sort($yourData, '/Video/id', 'asc');

.:)

0

Source: https://habr.com/ru/post/1709547/


All Articles