0

と の 2 つのデータベース テーブルがあるimagesとしalbumsます。次のスキーマがあります。

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

albums:
id, hash, name, description, user_id

テーブル内のalbum_idフィールドは、テーブル内のフィールドをimages指します。かなり単純な 1 対多のもの。画像は 1 つのアルバムに属することができ、アルバムには多くの画像を含めることができます。私が混乱しているのは、CakePHPモデルで指定されたアルバムから画像を取得する方法です。現時点では、これは私のモデルです:idalbumsImage

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

私がやりたいことは、アルバム ハッシュ (hashテーブル内のフィールド) に基づいて特定のアルバムから画像を返すことです。アルバム ID があればこれを機能させる方法はわかっていますが、わかりません。ハッシュしかありません。

私はすでにこれだけのアルバムモデルを持っています:

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

しかし、それが関連しているかどうかさえわかりません。:p

ありがとう。

4

1 に答える 1

0

これを試して:

if (!empty($options['albumHash'])) {
    array_push($params['conditions'], array('Album.hash' => $options['albumHash']));
}
于 2012-07-12T15:42:49.467 に答える