我有一个雇主的员工。在我的员工表中,我有一个名为 employer_id 的字段。使用此信息的报告正在我的本地开发设置上工作,但不适用于我的虚拟主机。我不知道是什么原因导致它不这样做。
当我尝试拉出所有员工的列表时,出现以下错误:
错误: SQLSTATE[42S22]: 找不到列: 1054 未知列 "where"子句中的"Employer.last_name">
SQL 查询:选择"
Employee
"。id
,Employee
.employer_id
,Employee
.first_name
,Employee
。last_name
,Employee
.Employee
.webinar_id
,Employee
。questions
,Employee
.correct_answers
,Employee
。required_to_pass
,Employee
.status
,Employee
。role
,Employee
。sport
,Employee
.program
,Employee
。created
,Employee
。modified
来自sportrisk_wp
.employees
作为Employee
(Employee
.last_name
不像 ''( 和 (Employer
.last_name
不是 像 ''(( 和contain
= '1' 按Employee
排序。created
DESC 限制 50
在我的本地副本上,当我将debug
更改为 2 时,我在 SQL 日志中看到以下内容(它正在加入雇主(:
选择
Employee
。id
,Employee
.employer_id
,Employee
.first_name
,Employee
。last_name
,Employee
.Employee
.webinar_id
,Employee
。questions
,Employee
.correct_answers
,Employee
。required_to_pass
,Employee
.status
,Employee
.role
,Employee
.sport
,Employee
.program
,Employee
.created
,Employee
。modified
,Employer
.id
,Employer
.employer_id
,Employer
。company
,Employer
.first_name
,Employer
。last_name
,Employer
。phone
,Employer
.Employer
。username
,Employer
。password
,Employer
.usercode
,Employer
.subscriber
,Employer
.active
,Employer
.admin
,Employer
。created
,Employer
。modified
,Employer
.status
来自local
.employees
作为左Employee
加入local
.users
Employer
上 (Employee
.employer_id
=Employer
.id
( 其中 (Employee
.last_name
不像"(和 (Employer
.last_name
不像 ''(( 按Employee
订购。created
DESC 限制 50
我相信这是所有相关的代码。
用户控制器
// inside a larger function
// Load Employee Model
$this->loadModel('Employee');
// Base conditions - to hide some old records which do not contain name information.
$conditions = array(
"Employee.last_name NOT LIKE" => '',
"Employer.last_name NOT LIKE" => ''
);
// Find records & paginate
$this->paginate = array(
'limit' => 50,
'conditions' => array(
"AND" => $conditions
),
'order' => array('Employee.created DESC')
);
$employees = $this->paginate('Employee');
?>
员工模式
<?php
class Employee extends AppModel
{
var $name = 'Employee';
var $recursive = 1;
var $belongsTo = array (
'Employer' => array (
'className' => 'Employer',
'foreignKey' => 'employer_id',
'order' => 'Employee.last_name ASC'
)
);
public $actsAs = array('Containable');
var $validate = array(
'email' => array(
'rule' => 'notBlank'
),
'first_name' => array(
'rule' => 'notBlank'
),
'last_name' => array(
'rule' => 'notBlank'
),
'usercode' => array(
'rule' => 'notBlank'
)
);
}
?>
雇主模式
<?php
class Employer extends AppModel
{
var $name = 'Employer';
var $recursive = -1;
var $hasOne = 'Supervisor';
var $hasMany = 'Employee';
public $useTable = 'users';
public $actsAs = array('Containable');
var $validate = array(
'email' => array(
'rule' => 'notBlank'
),
'password' => array(
'rule' => 'notBlank'
)
);
}
?>
我不知道为什么它不在一个环境中链接雇主模型,而是在另一个环境中链接。我已经完成了所有文件的直接副本。
SQL 查询
这是我在托管网站上得到的查询
SELECT `Employee`.`id`, `Employee`.`employer_id`, `Employee`.`first_name`, `Employee`.`last_name`, `Employee`.`email`, `Employee`.`webinar_id`, `Employee`.`questions`, `Employee`.`correct_answers`, `Employee`.`required_to_pass`, `Employee`.`status`, `Employee`.`role`, `Employee`.`sport`, `Employee`.`program`, `Employee`.`created`, `Employee`.`modified` FROM `sportrisk_wp`.`employees` AS `Employee` WHERE ((`Employee`.`last_name` NOT LIKE '') AND (`Employer`.`last_name` NOT LIKE '')) ORDER BY `Employee`.`created` DESC LIMIT 50
这是我在本地网站上得到的(也是我正在寻找的(
SELECT `Employee`.`id`, `Employee`.`employer_id`, `Employee`.`first_name`, `Employee`.`last_name`, `Employee`.`email`, `Employee`.`webinar_id`, `Employee`.`questions`, `Employee`.`correct_answers`, `Employee`.`required_to_pass`, `Employee`.`status`, `Employee`.`role`, `Employee`.`sport`, `Employee`.`program`, `Employee`.`created`, `Employee`.`modified`, `Employer`.`id`, `Employer`.`employer_id`, `Employer`.`company`, `Employer`.`first_name`, `Employer`.`last_name`, `Employer`.`phone`, `Employer`.`email`, `Employer`.`username`, `Employer`.`password`, `Employer`.`usercode`, `Employer`.`subscriber`, `Employer`.`active`, `Employer`.`admin`, `Employer`.`created`, `Employer`.`modified`, `Employer`.`status` FROM `local`.`employees` AS `Employee` LEFT JOIN `local`.`users` AS `Employer` ON (`Employee`.`employer_id` = `Employer`.`id`) WHERE ((`Employee`.`last_name` NOT LIKE '') AND (`Employer`.`last_name` NOT LIKE '')) ORDER BY `Employee`.`created` DESC LIMIT 50
最终结果 最后(在与 Ved 聊天后(我的分页数组最终变成了这个,并且我的报告加载了。
$this->paginate = array(
'contain' => array('Employer'),
'limit' => 50,
'conditions' => array(
"AND" => $conditions
),
'joins' => array( array( 'alias' => 'Employer', 'table' => 'users', 'type' => 'LEFT', 'conditions' => 'Employer.id = Employee.employer_id' ) ),
'fields' => array('Employee.first_name', 'Employee.last_name', 'Employee.email', 'Employee.webinar_id', 'Employee.questions', 'Employee.correct_answers', 'Employee.required_to_pass', 'Employee.status', 'Employee.role', 'Employee.sport', 'Employee.program', 'Employee.created', 'Employer.id', 'Employer.employer_id', 'Employer.company', 'Employer.first_name', 'Employer.last_name', 'Employer.email', 'Employer.active'),
'order' => array('Employee.created DESC')
);
我想你忘记加载关联数据,使用包含
// Find records & paginate
$this->paginate = array(
'contain' => array('Employer'),
'fields' => array('Employer.id'), // add more fields
'limit' => 50,
'conditions' => array(
"AND" => $conditions
),
'joins' => array( array( 'alias' => 'Employer', 'table' => 'users', 'type'
=> 'LEFT', 'conditions' => 'Employer.id = Employee.employer_id' ) ),
'order' => array('Employee.created DESC')
);