显示相关产品,从网格中排除当前产品

Show related products, exclude current product from grid

我在 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);
}

您需要从相关作品中排除艺术品:

'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);
}