Cakephp - sort by second level association in paginate

I play with a database of quotes related to the ski run I am running. I try to list the quotes, but sort by the person who said the quote, and try my best for the paginate helper to let me do this.

I have four tables.

quotes, trips, people and attendance. Attendance is essentially a join table for people and travel.

The relationship is as follows:

Attendance depends on the person hasMany Attendance
A visit belongs to a multi-user trip
Attendance hasMany Quote belongs to an attendance

In QuotesController, I use canable to retrieve fields from Quote, as well as related Attendance and fields from Trip and Person associated with this visit.

function index() {
   $this->Quote->recursive = 0;
   $this->paginate['Quote'] = array(
   'contain' => array('Attendance.Person', 'Attendance.Trip'));
   $this->set('quotes', $this->paginate());
}

, , , ,

foreach ($quotes as $quote) {
echo $quote['Attendance']['Person']['first_name'];
}

.

, , - / , paginate

 echo $this->Paginator->sort('Name', 'Attendance.Person.first_name');

echo $this->Paginator->sort('Location', 'Attendance.Trip.location');

. , - , , .

$quotes, , :

Array
(
     [0] => Array
            (
                [Quote] => Array
                     (
                        [id] => 1
                        [attendance_id] => 15
                        [quote_text] => Hello

            )

                [Attendance] => Array
            (
                        [id] => 15
                        [person_id] => 2
                        [trip_id] => 7
                        [Person] => Array
                             (
                              [id] => 2
                              [first_name] => John
                              [last_name] => Smith

                               )

                        [Trip] => Array
                             (
                              [id] => 7
                              [location] => La Plagne
                              [year] => 2000
                              [modified] => 
                         )

                 )

         )

, - , Person, . , , . ?

cakephp, , , .

.

+3
3

. . .

echo $this->Paginator->sort('Name', 'Attendance.Person.first_name');

:

echo $this->Paginator->sort('Name', array('Attendance' => 'Person.first_name'));

,

+1

.

, https://github.com/Terr/linkable. , . . , .

    $this->paginate = array(
        "recursive"=>0,
        "link"=>array("Issue"=>array("Publication")),
        "order"=>array("Publication.name"=>"ASC",
        "limit"=>10);

$this- > Paginator- > params- > paging- > Clipping , , : "defaults" "options". , , . :

[Clipping] => Array
            (
                [page] => 1
                [current] => 10
                [count] => 6685
                [prevPage] => 
                [nextPage] => 1
                [pageCount] => 669
                [defaults] => Array
                    (
                        [limit] => 10
                        [step] => 1
                        [recursive] => 0
                        [link] => Array
                            (
                                [Issue] => Array
                                    (
                                        [0] => Publication
                                    )

                            )

                        [order] => Array
                            (
                                [Publication.name] => ASC
                            )

                        [conditions] => Array
                            (
                            )

                    )

                [options] => Array
                    (
                        [page] => 1
                        [limit] => 10
                        [recursive] => 0
                        [link] => Array
                            (
                                [Issue] => Array
                                    (
                                        [0] => Publication
                                    )

                            )

                        [order] => Array
                            (
                                [Publication.name] => ASC
                            )

                        [conditions] => Array
                            (
                            )

                    )

            )

$this- > Paginator- > sort ( "", "." );

, .

[Clipping] => Array
            (
                [page] => 1
                [current] => 10
                [count] => 6685
                [prevPage] => 
                [nextPage] => 1
                [pageCount] => 669
                [defaults] => Array
                    (
                        [limit] => 10
                        [step] => 1
                        [recursive] => 0
                        [link] => Array
                            (
                                [Issue] => Array
                                    (
                                        [0] => Publication
                                    )

                            )

                        [order] => Array
                            (
                                [Publication.name] => DESC
                            )

                        [conditions] => Array
                            (
                            )

                    )

                [options] => Array
                    (
                        [page] => 1
                        [limit] => 10
                        [recursive] => 0
                        [link] => Array
                            (
                                [Issue] => Array
                                    (
                                        [0] => Publication
                                    )

                            )

                        [order] => Array
                            (
                            )

                        [conditions] => Array
                            (
                            )

                    )

paginator, ?


UPDATE:


: paginator . . , , . paginate app_controller.php , : ( 1172 cake/libs/controller/controller.php)

if(empty($options["order"])){
unset($options["order"]);
}

. , .php, app_controller.php .

0

CakePHP 3 , 'sortWhitelist' params $this->paginate .

$this->paginate = [
    // ...
    'sortWhitelist' => ['id', 'status', 'Attendance.Person.first_name']
];

, :

echo $this->Paginator->sort('Name', 'Attendance.Person.first_name');

:

This option is required if you want to sort any related data or calculated fields that may be part of your page request:

However, this can easily be missed by tired eyes, so I hope this helps someone out there!

0
source

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


All Articles