簡體   English   中英

CakePHP 2.2.4:后續安裝

[英]CakePHP 2.2.4: Afterfind Setup

我在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 )。

這是我的ARTICLES.PHP CONTROLLER

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 ,我沒有收到任何錯誤消息。如果我在print_r($article['User'])print_r($comment['User']) ,則遞歸= 3 view.ctp,兩個數組都顯示正確的在線值。

遞歸= 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
)

如果我以遞歸= 3 echo $article['User']['online'] ,我將得到正確的值(需要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

可能的解決方案:進行了一些修改之后,我發現以下方法(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));

嘗試跟隨foreach

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

請注意,這只會返回其中user_id = 1的計數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM