Association filter conditions

I have a search function that works great for staff, so I can search by name. Now, I want the filter staff to be created by the staffgroup.groupname team, but unfortunately I get this error:

Column not found: 1054 Unknown column 'staffgroups.groupname' in 'where clause'

I have the following tablelayout

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

i used the following conditions:

$tmpConditions['AND'][] = array('Staff.isActive =' => "1");
$tmpConditions['OR'][] =  array('Staff.lastname LIKE' => "%$name%");
$tmpConditions['OR'][] = array('staffgroups.groupname LIKE' => "%$group%");
[...]

$this->Staff->recursive = 1;
$this->paginate = array('conditions' =>  $tmpConditions );
$this->set('staffs', $this->paginate());

I am unable to get it to work, although I think the condition is set correctly.

cheers endo

+1
source share
4 answers
$tmpConditions['OR'][] = array('staffgroups.groupname LIKE' => "%$group%");

CakePHP agreement does not follow . It should be:

$tmpConditions['OR'][] = array('StaffGroup.group_name LIKE' => "%$group%");

:

:

CamelCased. Person, BigPerson ReallyBigPerson - .

:

, CakePHP, . , big_people really_big_people, .

:

, first_name.

, staff_groups, group_name. StaffGroup.

+1

" " - HABTM. .

StaffGroup.id(),

$group_ids = $this->Staff->StaffGroup->field('id', array('groupname LIKE' => '%$group%') );

HABTM, hasMany association

$this->Staff->UnbindModel( array('hasAndBelongsToMany' => array('StaffGroup')) );
$this->Staff->bindModel(array('hasMany' => array('StaffStaffGroup')));

$this->Staff->StaffStaffGroup->find(
    'all', 
    array(
        'conditions' => array(
            'StaffStaffGroup.staff_group_id' => $group_ids,
            'Staff.isActive =' => "1",
            'Staff.lastname LIKE' => "%$name%",
        )
    )
);
+1

$tmpConditions :

$tmpConditions = array(
       "AND" => array('Staff.isActive =' => "1"),
       "OR"  => array(
                'Staff.lastname LIKE' => "%$name%",
                'Staffgroup.groupname LIKE' => "%$group%"
       )
);

, staffgroups groupname.

$this->Staff->recursive = 1;

$this->paginate = array(
      ...
      'recursive' => 1
);

, ...

0
0

All Articles