CakePHP 在本地链接模型,而不是在托管副本上?

CakePHP is linking models locally, but not on hosted copy?

我的员工只有一个雇主。在我的员工 table 中,我有一个名为 employer_id 的字段。使用此信息的报告适用于我的本地开发设置,但不适用于我的虚拟主机。我不知道是什么原因导致的。

当我尝试提取所有员工的列表时,出现以下错误:

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

SQL Query: 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 '')) AND contain = '1' ORDER BY Employee.created DESC LIMIT 50

在我的本地副本上,当我将 debug 更改为 2 时,我在 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, 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

我相信这是所有相关代码。

用户控制器

// 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'
        )
    );
}
?>

我不明白为什么它不在一个环境中链接 Employer 模型,而是在另一个环境中链接。我已经完成了所有文件的直接复制。

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

我想你忘记加载关联数据了,使用contain

// 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')
);