CakePHP 2.2.4:Afterfind Setup

时间:2012-12-11 08:40:28

标签: php cakephp

我遇到了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: 0Cannot use string offset as an arrayColumn User.online not found

编辑:也许这会更深入地了解这个问题。

我正在尝试查看ARTICLES VIEW.CTP。 它会在顶部显示包含$article['User']信息的文章(尝试包含$article['User']['online'])。帖子下面是文章评论(foreach $article['Comment'] as $comment)。

这是我的文章.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.online时数组和INSTEAD设置为$this->Article->recursive = 3,我没有收到任何错误消息。对于recursive = 3,如果我在文章view.ctp上print_r($article['User'])print_r($comment['User']),则数组都显示正确的在线值。

带有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
)

如果我echo $article['User']['online']的recursive = 3,我会得到正确的值(需要3才能显示UserAvatar信息)。

我不明白为什么,然后,使用包含,是否说无法找到User.online?我的AppModel中有actAs = 'Containable',因为我几乎在所有模型上使用该属性。

这是我最新的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

可能的解决方案:经过多一点修改后我发现以下工作(文章模型)但这并不理想,因为它从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));

1 个答案:

答案 0 :(得分:0)

尝试关注foreach

foreach($results as &$val){
    $val['User']['online'] = $this->UsersOnline->find('count', array(
        'conditions' => array('UsersOnline.user_id' => 1)
    ));
}

请注意,这只会返回user_id = 1

的计数