I have a search function, which works great for staff, so I can search by name. Now, I want filter staffs by staffgroup.groupname, but unfortunatelly i get this error:
Column not found: 1054 Unknown column 'staffgroups.groupname' in 'where clause'
I'm having the following tablelayout
- staffs (a person can belong to many groups)
- staff_staffgroups (HABTM-linking table)
- staffgroups (has a groupname)
i used the conditions as follows:
$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 cant manage to get it working, allthough i think the Condition is set right.
cheers endo
$tmpConditions['OR'][] = array('staffgroups.groupname LIKE' => "%$group%");
Is not following the CakePHP conventions. It should be:
$tmpConditions['OR'][] = array('StaffGroup.group_name LIKE' => "%$group%");
Model and Database conventions:
Models:
Model classnames are singular and CamelCased. Person, BigPerson, and ReallyBigPerson are all examples of conventional model names.
Database tables:
Table names corresponding to CakePHP models are plural and underscored. The underlying tables for the above mentioned models would be people, big_people, and really_big_people, respectively.
Database fields:
Field names with two or more words are underscored like, first_name.
Following these conventions, you should have a table called staff_groups
which contains a field called group_name
. The associated model would be named StaffGroup
.
You cannot do that "out of the box", due to the HABTM association. You have to do it by hand.
First you need to get the StaffGroup.id(s) you are looking for
$group_ids = $this->Staff->StaffGroup->field('id', array('groupname LIKE' => '%$group%') );
Then unbind the HABTM association, and then bind the join table as hasMany association
$this->Staff->UnbindModel( array('hasAndBelongsToMany' => array('StaffGroup')) );
$this->Staff->bindModel(array('hasMany' => array('StaffStaffGroup')));
You can now perform your search
$this->Staff->StaffStaffGroup->find(
'all',
array(
'conditions' => array(
'StaffStaffGroup.staff_group_id' => $group_ids,
'Staff.isActive =' => "1",
'Staff.lastname LIKE' => "%$name%",
)
)
);
In your $tmpConditions
array the you should have the following format:
$tmpConditions = array(
"AND" => array('Staff.isActive =' => "1"),
"OR" => array(
'Staff.lastname LIKE' => "%$name%",
'Staffgroup.groupname LIKE' => "%$group%"
)
);
I assume that you have a table named staffgroups
and that it actually contains the field groupname
.
Also the line
$this->Staff->recursive = 1;
should be
$this->paginate = array(
...
'recursive' => 1
);
I believe that this should do the job...
Just change staffgroups
to Staffgroup
. Naming conventions for models vs. tables means db table my_tables
will be named model MyTable
. See http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions.
来源:https://stackoverflow.com/questions/14562932/filter-conditions-from-an-association