0

私たちの Yii Framework アプリケーションには、 UserProfileImages モデルの一部として次のものが定義されています。

public function getProfileImages($param, $user_id) {
        if(isset($param['select']) && $param['select']=='all'){
            $profile_images = UserProfileImages::model()->findAllByAttributes( array( 'user_id'=>$user_id) );

        } else {
            $profile_images = UserProfileImages::model()->findByAttributes( array( 'user_id'=>$user_id) );
        }
        return $profile_images;
    }

上記のスニペットをビュー内のウィジェットに接続して、特定のユーザーのすべての画像を返すにはどうすればよいですか?
おまけの質問: 上記をレンダリングするには、どの画像回転子をお勧めしますか?

4

1 に答える 1

1

コントローラーが $user_id を指定したと仮定して、ビュー ファイルに次のようなものを追加します。

$this->widget('UserProfileImagesWidget', array(
    "userProfileImages" => UserProfileImages::getProfileImages(
        array("select" => "all"), 
        $user_id
     ),
     "user_id" => $user_id
)); 

MVC の考え方によっては、コントローラーで userProfileImages データを取得し、そのデータをビューに渡すこともできます。

次のようにウィジェットを定義します。

class UserProfileImagesWidget extends CWidget {
    public $user_id;
    public $userProfileImages = array();

    public function run() {
        $this->render("userProfileImage");
    }
}

最後に、userProfileImages.php ビュー ファイルで、次のようなことができます。

if(!empty($this->userProfileImages)) {
    // Your display magic
    // You can access $this->user_id
}

補足として: getProfileImages でパラメーターの順序を変更したい場合があります。$user_id が最初のパラメーターである場合、何も指定したくない場合は $params を完全に省略できます。

于 2012-08-24T09:03:49.287 に答える