Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditions to paginate for belongsToMany CakePHP 3

I have the tables Semesters, Disciplines and a jointTable Semesters_Disciplines. I want to create a action index in DisciplinesController with a semester_id as parameter, which list with paginate just the disciplines what belongs to the semester with the id passed in the parameter. I tried this:

public function index($semester_id)
{
    $options = ['semester_id' => $semester_id];
    $this->paginate = ['conditions' => $options];

    $this->set('disciplines', $this->paginate($this->Disciplines));
    $this->set('_serialize', ['disciplines']);
}
like image 502
Luiz Avatar asked Apr 20 '15 00:04

Luiz


People also ask

What is CakePHP paginate?

CakePHP eases the burden on the developer by providing a quick, easy way to paginate data. Pagination in CakePHP is offered by a component in the controller, to make building paginated queries easier. In the View PaginatorHelper is used to make the generation of pagination links & buttons simple.

What is contain in CakePHP?

A new addition to the CakePHP 1.2 core is the ContainableBehavior . This model behavior allows you to filter and limit model find operations. Using Containable will help you cut down on needless wear and tear on your database, increasing the speed and overall performance of your application.


1 Answers

You'll have to use a query that uses matching or joins to be able to filter on non 1:1/n-1 associations.

You can do so by either passing a query directly to the paginate() method

// ...
$this->set('disciplines', $this->paginate(
    $this->Disciplines
        ->find()
        ->matching('Semesters', function(\Cake\ORM\Query $q) use ($semester_id) {
            return $q->where([
                'Semesters.id' => $semester_id
            ]);
        })
        ->group(['Disciplines.id'])
));
// ...

or by using a custom finder.

// ...
$this->paginate = [
    'finder' => [
        'semesters' => [
            'semester_id' => $semester_id
        ]
    ]
];
$this->set('disciplines', $this->paginate($this->Disciplines));
// ...
// DisciplinesTable
public function findSemesters(\Cake\ORM\Query $query, array $options)
{
    $query
        ->matching('Semesters', function(\Cake\ORM\Query $q) use ($options) {
            return $q->where([
                'Semesters.id' => $options['semester_id']
            ]);
        })
        ->group(['Disciplines.id']);
    return $query;
}

See also

  • Cookbook > Pagination > Using Controller::paginate()
  • Cookbook > Retrieving Data and Result Sets > Custom Finder Methods
  • Cookbook > QueryBuilder > Filtering by Associated Data
  • Cookbook > QueryBuilder > Adding Joins
like image 152
ndm Avatar answered Sep 23 '22 21:09

ndm