afterfind() に問題があり、読んでいるものはどれも解決していません。これは、関連するモデルで 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
またはの 3 つのエラーのうちの 1 つが発生しますColumn User.online not found
。
編集:多分これは問題へのもう少しの洞察を提供します.
ARTICLES 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
コードをそのまま使用すると (「contain」条件を見ると、エラー メッセージ " Column User.online does not exist
" が返されます。ただし、Comment 配列と User 配列および INSTEAD セットの両方で、contain から User.online を削除すると、no が返され$this->Article->recursive = 3
ますエラー メッセージ. recursive = 3 の場合、Iprint_r($article['User'])
またはprint_r($comment['User'])
article view.ctp の場合、配列は両方とも online の正しい値を示します。
recursive = 3 の $article['User'] と $comment['User'] の両方の正確な出力は次のとおりです。
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
)
recursive = 3 の場合echo $article['User']['online']
、正しい値が得られます (UserAvatar 情報を表示するには 3 が必要です)。
では、contain を使用すると、User.online が見つからないと表示される理由がわかりません。actAs = 'Containable'
ほぼすべてのモデルでそのプロパティを使用しているため、AppModel に含まれています。
これが私の最新の USER.PHP MODEL 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
考えられる解決策: もう少しいじってみると、次のように機能することがわかりました (Articles モデル) が、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));