Filter HABTM-associated model with conditions

preamble: a few days ago I asked a question to solve the HABTM filter, I can’t do it even with textbooks, so "Obi Kwan Kenobi youre my only hope."

What I want to achieve: Filtering personnel by GroupID, which is used by StaffStaffgroup

I have the following tablelayout

  • employees (a person can belong to many groups)
  • staff_staffgroups (HABTM-linking table)
  • staffgroups (has a group name)

The $ tmp variable returns me a working array, but the problem is that Staff is a child of StaffStaffgroups. I could parse and assemble the array, but this is not a good solution. So I want to use the condition for Staff (see Command Prompt), but then I get error 1054 "column not found: 1054 Unknown column". I tried to tie and untie, but there is no result.

$group_id = 2;
$tmp = $this->Staff->StaffStaffgroup->find('all',
        array('conditions' => array(
            'StaffStaffgroup.staffgroup_id' => $group_id,
            'Staff.isActive =' => "1",
            'Staff.last_name LIKE' => "%$name%",
            )
         )
);

debug($tmp);

//$tmpConditions['AND'][] = array('StaffStaffgroup.staffgroup_id' => $group_ids);

EDIT:

I tried this with conditions and restrained behavior, but unfortunately it didn't filter anything at all

    $this->Staff->contain(array('StaffStaffgroup'));
    $this->paginate = array('StaffStaffgroup' =>array(
                                    array('conditions'  => array(
                                            'StaffStaffgroup.staffgroup_id' => '2'
                                        )
                                    )
                                )
    );
  • I added to all models: public $ actAs = array ('Containable');
  • I tried also with the inner join, but there was no filtering:

     $this->paginate = array( 
     'conditions' => array('StaffStaffgroup.staffgroup_id' => 2 ),
     'joins' => array(
        array(
            'alias' => 'StaffStaffgroup',
            'table' => 'staff_staffgroups',
            'type' => 'INNER',
            'conditions' => 'StaffGroup_id = StaffStaffgroup.staffgroup_id'
        )
     )
    

    );

+5
source share
3 answers

Containable. sql, , .

StaffStaffgroup , .

$staff_ids = $this->Staff->StaffStaffgroup->find(
    'all', 
    array(
        'conditions' => array('StaffStaffgroup.staffgroup_id' => $group_id, ),
        'recursive' => -1,
        'fields' => array('StaffStaffgroup.staff_id'),
    )
);

,

$staffs = $this->Staff->find(
    'all', 
    array(
        'conditions' => array(
            'Staff.id' => $staff_ids, 
            'Staff.isActive =' => "1",
            'Staff.last_name LIKE' => "%$name%",
        ),
    )
);
+5

, , - .

, HABTM- , HABTM-.

:

<?php
class Product extends AppModel {

    public $actsAs = array(
        'FilterHabtm.FilterHabtm',
        'Containable' // If you use containable it very important to load it AFTER FilterHabtm
    );

    public $hasAndBelongsToMany = array(
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'product_id',
            'associationForeignKey' => 'category_id',
            'with' => 'CategoryProduct'
        )
    );

}

:

( )

<?php
class ProductsController extends AppController {

    public $name = 'Products';

    public function index($categoryId = null) {
        $products = $this->Product->find('all', array(
            'conditions' => array(
                'Category.id' => $categoryId // Filter by HABTM conditions
            )
        ));
    }

}

Please view this URL for downloads and complete usage instructions: https://github.com/biesbjerg/FilterHabtm

+1
source

All Articles