JOINed表中的条件显示错误CakePHP

时间:2014-04-10 12:45:47

标签: sql cakephp join where

我有两张表employee_personals,其中存储了员工的所有个人记录,并且telephone_bills每月存储支付给特定员工的电话费。现在我的employeePersonalsController.php我有一个名为api_show_employees()的函数,类似于下面的函数:

function api_show_employees() {
      //$this->autoRender = false;
      //Configure::write("debug",0);

      $office_id = '';
      $cond = '';

      if(isset($_GET['office_id']) && trim($_GET['office_id']) != '') {
        $office_id = $_GET['office_id'];
        $cond['EmployeePersonal.office_id'] = $office_id;
      }


      if(isset($_GET['telephoneBillTo']) && isset($_GET['telephoneBillFrom']) ) {
        if($_GET['telephoneBillTo'] != '' && $_GET['telephoneBillFrom'] != '') {
          $cond['TelephoneBill.bill_from'] = $_GET['telephoneBillFrom'];
          $cond['TelephoneBill.bill_to'] = $_GET['telephoneBillTo'];
        }
      }


      $order = 'EmployeePersonal.name';
     // $employee = $this->EmployeePersonal->find('all');
      $employee = $this->EmployeePersonal->find('all',array('order' => $order,'conditions'=>$cond));
      //return json_encode($employee);
    }

此功能基本上可以找到在给定时间内支付账单的所有员工。但是我收到了一个错误

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'TelephoneBill.bill_from' in 'where clause'

模特:EmployeePersonal.php

var $hasMany = array(
  'TelephoneBill' => array(
         'className' => 'TelephoneBill',

     )
);

TelephoneBill.php

public $name = 'TelephoneBill';
var $hasMany = array('EmployeePersonal');

注意:如果我跳过bill_frombill_to条件,我会得到TelephoneBill数组的结果!

1 个答案:

答案 0 :(得分:1)

TLDR:改为使用Joins


<强>详情/说明:

1)您似乎正在使用recursive。不要这样做。请改用Containable

2)您不能根据来自包含/递归包含表的数据的条件来限制父模型 - 而是使用Joins

2b)或者,您可以从其他方向进行查询,并使用条件查询TelephoneBill,然后包含EmployeePersonal