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

时间:2016-04-11 13:42:16

标签: php cakephp cakephp-2.0 has-and-belongs-to-many

我有一个在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 个答案:

答案 0 :(得分: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);
}