小编典典

JOINed表中的条件显示错误CakePHP

sql

我有两个表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条件,我将得到TelephoneBillarray的结果!


阅读 162

收藏
2021-04-14

共1个答案

小编典典

详细信息/注释:

1)看起来您正在使用recursive。不要那样做改用Containable

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

2b)或者,您可以从另一个方向查询,并查询TelephoneBill条件,然后包含EmployeePersonal

2021-04-14