2016-04-11 154 views
1

我有一個顯示Cakephp2中相關產品的網格,它的工作原理很好,但是相同的產品在相關產品的網格中顯示,我如何排除它?顯示相關產品,從網格中排除當前產品

這裏是我的作品控制器代碼:

public function view($id = null) { 
    if (!$this->Artwork->exists($id)) { 
     throw new NotFoundException(__('Invalid artwork')); 
    } 
    $options = array('conditions' => array(
     'Artwork.' . $this->Artwork->primaryKey => $id), 
     'recursive' => 0); 
    $artwork = $this->Artwork->find('first', $options); 
    $this->set('artwork', $artwork); 

    // related artworks 
    $status = 'Artwork.status'; 
    $id = 'Artwork.artist_id'; 
    $related = $this->Artwork->find('all', 
     array(
      'limit' => 4, 
      'conditions' => array(
      $status => 1, 
      $id => $artwork['Artwork']['artist_id']))); 

    $this->set('artworks', $related); 
} 

回答

1

你需要從相關排除藝術品:

'Artwork.id !=' => $artwork['Artwork']['id'] 

試試這個:

public function view($id = null) { 
if (!$this->Artwork->exists($id)) { 
    throw new NotFoundException(__('Invalid artwork')); 
} 
$options = array('conditions' => array(
    'Artwork.' . $this->Artwork->primaryKey => $id), 
    'recursive' => 0); 
$artwork = $this->Artwork->find('first', $options); 
$this->set('artwork', $artwork); 

// related artworks 
$status = 'Artwork.status'; 
$id = 'Artwork.artist_id'; 
$related = $this->Artwork->find('all', 
    array(
     'limit' => 4, 
     'conditions' => array(
     $status => 1, 
     $id => $artwork['Artwork']['artist_id'], 
     'Artwork.id !=' => $artwork['Artwork']['id'] 
))); 


$this->set('artworks', $related); 
} 
+1

你是一個天才,謝謝 –

相關問題