doulang1945 2012-07-12 15:39
浏览 32
已采纳

使用CakePHP从特定相册返回图像

Consider I have two database tables: images and albums. They have these schemas:

images:
id, orig_name, hash, filename, uploaded, views, album_id, user_id, server_id

albums:
id, hash, name, description, user_id

The album_id field in the images table points to the id field in the albums table. Pretty simple one-to-many stuff. An image can belong to one album and an album can have many images. What I'm confused about is how to get images from a specified album in my CakePHP Image model. At the moment this is my model:

class Image extends AppModel {
    public $name = 'Image';
    public $belongsTo = array('Server', 'Album');

    public function getImages($options) {
        $params = array('conditions' => array());

        // Specific user
        if (!empty($options['userID'])) {
            array_push($params['conditions'], array('Image.user_id' => $options['userID']));
        }

        // Specific album
        if (!empty($options['albumHash'])) {
            array_push($params['conditions'], array('Image.album_id' => $options['albumHash']));
        }

        // Order of images
        $params['order'] = 'Image.uploaded DESC';
        if (!empty($options['order'])) {
            $params['order'] = $options['order'];
        }

        return $this->find('all', $params);
    }
}

What I want it to do is return images from a specific album based on the album hash (the hash field in the table). I know how I can make this work if I had the album ID, but I don't, I only have the hash.

I already have an album model that's just this:

class Album extends AppModel {
    public $name = 'Album';
}

but I'm not even sure if that's relevant. :p

Thanks.

  • 写回答

1条回答 默认 最新

  • douyi2107 2012-07-12 15:42
    关注

    Try this:

    if (!empty($options['albumHash'])) {
        array_push($params['conditions'], array('Album.hash' => $options['albumHash']));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?