【问题标题】:CakePHP: Joined Table Data Not Displaying in ViewCakePHP:连接表数据未显示在视图中
【发布时间】:2016-07-21 15:14:32
【问题描述】:

我有两个模型链接在 hasMany/belongsTo 关系中。这是 hasMany 的定义:

//Table hr_emp_ids. Each employee can have many HR cases.
public $hasMany = array(
    'HrCase' => array(
        'className' => 'HrCase',
        'foreignKey' => 'emp_user_id'
        )
    );

这里是 belongsTo 定义:

//Table hr_cases. Each HR case is owned by an employee.
public $belongsTo = array(
    'HrEmpId' => array(
        'className'=> 'HrEmpId',
        'foreignKey' => 'emp_user_id'
    );

我的视图的控制器非常简单:

public function view($id = null) {      
    $this->HrCase->id = $id;
    if (!$this->HrCase->exists()) {
        throw new NotFoundException(__('Invalid Case ID'));
    }
    $options = array('conditions' => array('HrCase.' . $this->HrCase->primaryKey => $id));
    $this->set('case', $this->HrCase->find('first', $options));
}

我要做的只是根据 hr_cases.emp_user_id = hr_emp_ids = emp_user_id 显示 hr_emp_ids 表中的hire_date 和 ssn。这是查看代码:

<tr>
<td><strong>Employee: </strong><br><?php echo h($case['HrCase']['full_name']); ?></td>
<td><strong>Date of Hire: </strong><br><?php echo h($case['HrEmpId']['hire_date']); ?></td>
<td><strong>SSN: </strong><br><?php echo h($case['HrEmpId']['ssn']); ?></td>

表结构:

desc hr_emp_ids;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| id          | int(11)     | NO   | PRI | NULL    | auto_increment |
| hire_date   | date        | YES  |     | NULL    |                |
| ssn         | varchar(11) | NO   |     | NULL    |                |
| emp_user_id | int(11)     | NO   |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+

desc hr_cases; (truncated)
+--------------------+-------------+------+-----+---------+----------------+
| Field              | Type        | Null | Key | Default | Extra          |
+--------------------+-------------+------+-----+---------+----------------+
| id                 | int(11)     | NO   | PRI | NULL    | auto_increment |
| emp_user_id        | int(11)     | NO   |     | NULL    |                |

HrEmpId 模型中的任何内容都不会显示。我不确定我在这里做错了什么。我已经做了几十次这样的联想,没有遇到任何麻烦。我可能会错过什么?

【问题讨论】:

    标签: php mysql sql cakephp


    【解决方案1】:

    我看不出相关数据未显示的任何直接原因。您在此处查找代码

    $options = array('conditions' => array('HrCase.' . $this->HrCase->primaryKey => $id));
    $this->set('case', $this->HrCase->find('first', $options));
    

    看起来可以通过使用getById() 来简化它,因为$options 中的唯一条件是检查$id。值得将 Containable Behavior 添加到 HrCase 模型,然后将 getById() 的第二个参数设置为 true。这应该允许您准确指定返回的关联数据。

    编辑:

    在 HrCase 模型中,您正在与外键 emp_user_id 建立 $belongsTo 关系,但这不是 hr_emp_ids 中的主键。 这就是不返回关联数据的原因。你需要这样设置:

    public $belongsTo = array(
        'HrEmpId' => array(
            'foreignKey' => false,
             'conditions' => array(
                 'HrEmpId.emp_user_id = HrCase.emp_user_id'
             ),
        )
    );
    

    更多信息请参见How to associate model in CakePHP by fields not named by convention?

    【讨论】:

    • 关于使用 findById() 的好建议。还向相关模型添加了可包含行为。控制器语句现在看起来像这样:$options = array($this-&gt;HrCase-&gt;findById($id), 'contain' =&gt; array('HrEmpId')); 不幸的是,当我调试 HrEmpId 时,数组是空的。还是被难住了!
    • 你用的是哪个版本的蛋糕?
    • 我们使用的是 2.0.5 版本。
    • 我刚刚对我的答案进行了编辑。如果它仍然不起作用,请告诉我。
    • 赢家鸡肉晚餐。谢谢。直到刚才我才意识到我在查看代码上方回答了我自己的问题!隐藏在众目睽睽之下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多