dongyu1918 2014-12-30 23:17
浏览 23

CakePHP中新HABTM的未定义索引

I’m changing a relationship, in a CakePHP web application, from a belongsTo to a HABTM.

Originally we had a department table and a user table. The department table could contain many users; but a user could only belong to one department.

Now we want the ability for a user to belong to multiple departments. I created an intermediary table called departments_users and I filled it full of data.

I've modified the user model, and now I'm getting this error in the users view:

Notice (8): Undefined index: Department [APP/views/users/index.ctp, line 25]

This is the line of code in the view that is giving the error:

<?php 
echo $this->Html->link($user['Department']['name'], 
array('controller' => 'departments', 'action' => 'view', $user['Department']['id'])
); 
?>

The above code uses the HtmlHelper to create a link with the name of the department (as the link text) and then supplies the department Id as a part of the link.

This is the code I've removed and added in the user model. First I removed this code from the $belongsTo relationship:

'Department' => array(
    'className'  => 'Department',
    'foreignKey' => 'department_id'
),

Then I added this code in the $hasAndBelongsToMany relationship:

'Department' => array(
    'className'             => 'Department',
    'joinTable'             => 'departments_users',
    'foreignKey'            => 'user_id',
    'associationForeignKey' => 'department_id',
    'unique'                => true
)

Could anyone tell what I could do to make this work? Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题