【问题标题】:CakePHP 2.2.4: Afterfind SetupCakePHP 2.2.4:事后设置
【发布时间】:2012-12-11 08:40:27
【问题描述】:

我遇到了 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'])。帖子下方是文章 cmets (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 数组的包含中删除 User.online 时, INSTEAD 设置$this->Article->recursive = 3,我没有收到任何错误消息。使用recursive = 3,如果我在文章view.ctp 上print_r($article['User'])print_r($comment['User']),数组都显示正确的在线值。

$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'] 使用递归 = 3,我会得到正确的值(需要 3 才能显示 UserAvatar 信息)。

我不明白为什么在包含时会说找不到 User.online?我的 AppModel 中有 actAs = 'Containable',因为我几乎在所有模型上都使用了该属性。

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

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    尝试关注foreach

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

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

    【讨论】:

    • 我在“$val['User']['online'] = $this->.....”这一行收到“不能使用字符串偏移量作为数组”
    • 你能发布 $val 的输出吗? $val 可能是一个字符串,在$this->User->field('name') 的情况下应该是它。在这种情况下,您应该跳过这段代码,因为您不需要它。
    • 我使用 user_id = 1 来简化事情。我不想在同一行出现两个不正确的变量。
    • 您还没有发布 $val 的输出。但是,我注意到当您不在字段列表中提供 id 和 foreign_key 时,CakePHP 没有正确包含更深层次的模型。你也可以试试这个。
    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多