我有两个表employee_personals,用于存储该员工的所有个人记录,并telephone_bills存储每个月支付给特定员工的电话费。现在employeePersonalsController.php我有一个名为的函数api_show_employees(),类似于以下内容:
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::
EmployeePersonal.php
var $hasMany = array( 'TelephoneBill' => array( 'className' => 'TelephoneBill', ) );
TelephoneBill.php
public $name = 'TelephoneBill'; var $hasMany = array('EmployeePersonal');
注意:如果我跳过bill_from和bill_to条件,我将得到TelephoneBillarray的结果!
bill_from
bill_to
TelephoneBill
详细信息/注释:
1)看起来您正在使用recursive。不要那样做改用Containable
recursive
2)您不能根据条件限制父模型包含的数据/包含递归的表中的数据- **而是使用Joins
2b)或者,您可以从另一个方向查询,并查询TelephoneBill条件,然后包含EmployeePersonal。
EmployeePersonal