【问题标题】:Show related products, exclude current product from grid显示相关产品,从网格中排除当前产品
【发布时间】:2016-04-11 13:42:16
【问题描述】:

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

【问题讨论】:

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


    【解决方案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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 2018-11-09
      • 2013-08-11
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多