我遇到了 afterfind() 的问题,而我正在阅读的内容都没有解决它。这与事后发现不适用于相关模型有关,我知道很多人提出了但我还没有找到解决方案。我看到的所有帖子也是2009-2010年的。
User.php 模型
function afterFind($results, $primary = false) {
parent::afterFind($results, $primary);
if($primary){
foreach($results as $key => $val){
if (isset($val['User']['id'])){
$results[$key]['User']['online'] = $this->UsersOnline->find('count',
array('conditions' => array('UsersOnline.user_id =' => $val['User']['id'])));
// THIS WORKS PERFECTLY FOR USER MODEL PAGES
}
}
}
else {
foreach($results as $key => $val){
$key['User']['online'] = $this->UsersOnline->find('count',
array('conditions' => array('UsersOnline.user_id =' => 1)));
// THIS IS THE PART THAT IS INCORRECT, AND THIS IS THE "BEST" SOLUTION
// I'VE FOUND ONLINE BUT IT IS NOT WORKING
/**
* I've also tried these
*
* $results[$key]['online']
* $results[$val]['online']
* $results[$key][$val]['online']
* $results['0']['User']['online']
*
* and about 5 other things
*/
}
}
return $results;
} // end afterfind
我在“else”语句中尝试了许多代码组合,但我得到了三个错误之一,即Uninitialized string offset: 0
,Cannot use string offset as an array
或Column User.online not found
.
编辑:也许这会为这个问题提供更多的洞察力。
我正在尝试查看文章 VIEW.CTP。它将带有信息的文章显示在顶部$article['User']
(试图包含$article['User']['online']
)。帖子下方是文章评论(foreach $article['Comment'] as $comment
)。
这是我的 ARTICLES.PHP 控制器
public function view($page = null, $id = null) {
// $this->Article->recursive = 3;
$this->Article->id = $id;
if (!$this->Article->exists()) {
$this->Session->setFlash('This article does not exist');
$this->redirect(array('controller'=>'articles', 'action' => 'index'), null, true);
}
$this->Article->contain(array(
'User' => array(
'fields'=>array('User.username', 'User.signature', 'User.online'),
'UserAvatar' => array(
'fields'=>array('UserAvatar.file')
)
),
'Comment' => array(
'fields'=>array('Comment.content', 'Comment.created', 'Comment.title'),
'User' => array(
'fields'=>array('User.username', 'User.signature', 'User.online'),
'UserAvatar' => array(
'fields'=>array('UserAvatar.file')
)
)
)
));
$this->set('article', $this->Article->read(null, $id));
} // end view function
使用代码原样(查看“包含”条件,我收到一条错误消息“ Column User.online does not exist
”。但是,当我从 Comment 数组和 User 数组和 INSTEAD set 的包含中删除 User.online 时$this->Article->recursive = 3
,我没有收到错误消息。使用 recursive = 3,如果我print_r($article['User'])
或print_r($comment['User'])
在文章 view.ctp 上,数组都显示正确的在线值。
$article['User'] 和 $comment['User'] with recursive = 3 的精确打印是
Array
(
[id] => this
[username] => this
[password] => ****
[UserAvatar] => Array
(
[id] => this
[column] => value
[column] => value
)
[OTHER USER RELATED MODELS] => Array
(
[column] => value
)
[online] => 1
[type] => 1
)
如果我echo $article['User']['online']
使用 recursive = 3,我会得到正确的值(需要 3 才能显示 UserAvatar 信息)。
我不明白为什么,然后,包含,它说 User.online 找不到?我actAs = 'Containable'
在我的 AppModel 中有,因为我在几乎所有模型上都使用了该属性。
这是我最新的 USER.PHP 模型 afterfind()
function afterFind($results, $primary = false) {
parent::afterFind($results, $primary);
if($primary){
foreach($results as $key => $val){
if (isset($val['User']['id'])){
$results[$key]['User']['online'] = $this->UsersOnline->find('count', array('conditions' => array('UsersOnline.user_id =' => $val['User']['id'])));
}
} // end for each
} // end if primary
else{
foreach($results as $key => $val){
if(isset($results['0']['User']['id'])){
$results['0']['User']['online'] = "1";
$results['User']['online'] = $results['0']['User']['online'];
$results['0']['User']['type'] = '1';
}
elseif(isset($results['User']['id'])){
$results['User']['online'] = "1";
$results[$key]['User']['online'] = $results['User']['online'];
$results['User']['type'] = '2';
} // end elseif
// everything returns as type 1 so this may be irrelevant
} // end for each
} // end if not primary
return $results;
} // end afterfind
可能的解决方案:经过更多修改后,我发现以下工作(文章模型)但这并不理想,因为它从 User 和 UserAvatar 检索所有数据,而我只想获取指定字段的数据。但是,当我使用 fields 选项时,无法识别 User.online。有任何想法吗?
$this->Article->contain(array(
'User' => array(
'UserAvatar'
),
'Comment' => array(
'User' => array(
'UserAvatar'
)
)
));
$this->set('article', $this->Article->read(null, $id));