Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cakephp 从关联中筛选条件_Cakephp_Foreign Keys_Conditional Statements_Model Associations - Fatal编程技术网

Cakephp 从关联中筛选条件

Cakephp 从关联中筛选条件,cakephp,foreign-keys,conditional-statements,model-associations,Cakephp,Foreign Keys,Conditional Statements,Model Associations,我有一个搜索功能,这对工作人员非常有用,所以我可以按姓名搜索。 现在,我希望按staffgroup.groupname筛选人员,但不幸的是,我得到了以下错误: Column not found: 1054 Unknown column 'staffgroups.groupname' in 'where clause' 我有下面的表格布局 员工(一个人可以属于多个组) 员工团队(HABTM链接表) staffgroups(具有组名) 我使用的条件如下: $tmpConditions['AN

我有一个搜索功能,这对工作人员非常有用,所以我可以按姓名搜索。 现在,我希望按staffgroup.groupname筛选人员,但不幸的是,我得到了以下错误:

Column not found: 1054 Unknown column 'staffgroups.groupname' in 'where clause'
我有下面的表格布局

  • 员工(一个人可以属于多个组)
  • 员工团队(HABTM链接表)
  • staffgroups(具有组名)

我使用的条件如下:

$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());
尽管我认为条件已经设置好,但我还是无法使它工作

干杯

$tmpConditions['OR'][] = array('staffgroups.groupname LIKE' => "%$group%");
他没有遵守规则。应该是:

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

型号:

模型类名是单数的,并且是大小写的。Person、BigPerson和ReallyBigPerson都是常规模型名称的示例

数据库表:

与CakePHP模型对应的表名是复数形式,并加下划线。上述模型的基础表分别是people、big_people和really_big_people

数据库字段:

带有两个或两个以上单词的字段名用下划线标出,例如,first_name


按照这些约定,您应该有一个名为
staff\u groups
的表,其中包含一个名为
group\u name
的字段。在
$tmpConditions
数组中,关联模型将命名为
StaffGroup
,您应具有以下格式:

$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
);

我相信这应该可以完成任务…

只需将
员工团队
更改为
员工团队
。模型与表格的命名约定意味着db table
my_表格
将被命名为模型
MyTable
。请参阅。

由于HABTM协会的原因,您无法“开箱即用”完成此操作。你必须手工做

首先,您需要获取要查找的StaffGroup.id

$group_ids = $this->Staff->StaffGroup->field('id', array('groupname LIKE' => '%$group%') );
然后取消绑定HABTM关联,然后将联接表绑定为HASMOUN关联

$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%",
        )
    )
);

你能更新你的问题吗?请将其添加到您的问题中,而不是编辑原始行。您是否可以澄清一下您的数据库结构是什么样的?你能访问其他数据吗?当你简单地
var_dump($this->StaffGroup->find('all');
会发生什么?我会重做mvc,然后再回复你。我不明白为什么
recursive
需要在paginate内部,而不是自己设置,两者都应该可以工作。这是文档中直接提到的[文档中哪里说
$this->Staff->recursive=1;
是错误的?在这一部分,它明确指出
recursive
以及其他
Model->find('all'))
相关参数通过
paginate
设置数组传递给分页模型。因此我相信这意味着你的答案是的,你可以通过paginate数组传递它,但仍然不必。这与CakePHP中查找查询的条件数组相同。你仍然可以使用
$this调用模型->Staff->recursive=1;
。这是正确的,而且会起作用。你自己试试看,你就会发现它起作用了!太糟糕了,我会试试。最后它仍然不起作用,尽管我得到了所有约定。我提出了一个后续问题: