我的网站上有一个功能,可记录各个字段的更改历史记录。用户的ID,文档ID,更改日期,信息更改等都被记录下来。
这是有效的代码。型号" Noncon" hasMany"历史":
public $hasMany = array(
'History' => array(
'className' => 'History',
'foreignKey' => 'noncon_id',
'dependent' => false,
'order' => 'date DESC',
)
外键noncon_id指向" id"在Noncon模型中。
模特"历史" belongsTo" Noncon":
public $belongsTo = array(
'Noncon' => array(
'className' => 'Noncon',
'foreignKey' => 'noncon_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'HistoryUser' => array(
'className' => 'User',
'foreignKey' => 'user',
'conditions' => '',
'fields' => '',
'order' => ''
)
HistoryUser已加入User模型。 HistoryUser.user = User.id。
观点:
foreach ($data['History'] as $history):
<td><?php echo $history['date']; ?></td>
<td><?php echo $history['HistoryUser']['full_name']; ?></td>
endforeach;
输出如下:
02/09/2017 07:58:40 AM John Doe
From: 2017-02-01
To: 2017-02-09
问题:
当我尝试在我的网站的另一部分中执行完全相同的事情时,我得到:
02/09/2017 07:58:40 AM Undefined index: HistoryUser [APP/View/Msrs/view.ctp, line 309]
From: 2017-02-01
To: 2017-02-09
同样,观点:
foreach ($data['MsrHistory'] as $history):
<td><?php echo $history['date']; ?></td>
<td><?php echo $history['HistoryUser']['full_name']; ?></td>
endforeach;
不会显示HistoryUser的全名。相反,我得到一个未定义的索引错误。以下是我试图开始实施的实施代码:
Model&#34; Msr&#34; hasMany&#34; MsrHistory&#34;:
public $hasMany = array(
'MsrHistory' => array(
'className' => 'MsrHistory',
'foreignKey' => 'msr_id',
'dependent' => false,
'order' => 'date DESC',
)
外键msr_id指向Msr模型中的id。
Model&#34; MsrHistory&#34; belongsTo&#34; Msr&#34;:
public $belongsTo = array(
'Msr' => array(
'className' => 'Msr',
'foreignKey' => 'msr_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'HistoryUser' => array(
'className' => 'User',
'foreignKey' => 'user',
'conditions' => '',
'fields' => '',
'order' => ''
),
当我为Noncon视图调试$ history变量时,我可以看到这些表都已加入。我得到以下输出:
[id] => 63838
[noncon_id] => 3221
[type] => Nonconformance
[date] => 2017-02-09 07:58:40
[user] => 75
[status] => startdate
[created] => 2017-02-09 07:58:40
[modified] => 2017-02-09 07:58:40
[from] =>
[to] => 2017-02-09
[created_user_id] => 75
[modified_user_id] => 75
[Noncon] => Array
(all of the Noncon fields)
[HistoryUser] => Array
(
[id] => 75
[group_id] => 1
[username] => Jness
[password] => d97ab2b5634dddf01a21adfb25da7c380327fb46
[auth_key] => 370e2934cc8ca7280e341347d99fdf3b
[first_name] => James
[last_name] => Ness
[full_name] => John Doe
[email_address] => john.doe@someplace.org
[login_attempts] => 0
[customer] => 0
[suspended] => 0
[created] => 2012-08-07 06:55:58
[created_user_id] => 12
[modified] => 2017-02-14 14:59:50
[modified_user_id] => 75
[deleted_record] => 0
[deleted_user_id] => 0
[deleted] => 0000-00-00 00:00:00
[dept_id] => 1
[sponsor_id] => 5
[is_manager] => 0
[ce_sig_auth] => 0
[is_logistics] =>
[is_budget] =>
)
但是当我在Msr视图中调试$ history变量时,我只得到:
Array
(
[id] => 1
[msr_id] => 5
[type] => Msr
[date] => 2017-03-31 12:44:30
[user] => 117
[status] => need_date
[created] => 2017-03-31 12:44:30
[modified] => 2017-03-31 12:44:30
[from] => 2017-04-25
[to] => 2017-04-04
[created_user_id] => 117
[modified_user_id] => 117
)
MSR字段和HistoryUser字段都不会显示。是什么赋予了?我可以确认所有数据都正确写入数据库。但我无法显示我的HistoryUser的全名。我的连接有什么不好的东西吗?