yii2将控制器操作中的最大ID设置为默认值

时间:2019-02-02 19:50:22

标签: php yii2

我有一个列出多个照片画廊的视图。 因此,从菜单中尝试访问图库视图时,操作是 site/gallery

现在在我的控制器操作中,我试图像这样显示最新添加的照片库。

这是我在控制器中的代码:

public function actionGallery($id = null)
{
    //$this->layout = "@app/themes/frontend/main";
    $activities = Activities::find()
        ->where(['status' => 1])
        ->orderBy(['id' => SORT_DESC])
        ->all();

    $model = Activities::find()->where(['id' => $id])->one();

    if ($id == null) {
        $gallery = Gallery::find()
            ->where(
                [
                    'status' => 'active',
                    'activity_name' => max(['id'])
                ]
            )->all();
    } else {
        $gallery = Gallery::find()
            ->where(
                ['status' => 'active', 'activity_name' => $id]
            )->all();
    }

    return $this->render(
        'gallery',
        [
            'gallery' => $gallery,
            'activities' => $activities,
            'model' => $model
        ]
    );
}

我也尝试过:

$gallery = Gallery::find()->where(
    [
        'status' => 'active',
        'activity_name' => max(['activity_name'])
    ]
)->all();

用于查看的代码:

<?php
    $this->title = 'gallery';
?>
<div class="container">
    <div class="col-lg-2 col-md-2 col-sm-12 col-xs-12" style='background-color:#fafafa'>
    <h3>Activities</h3>
    <?php
        foreach ($activities as $activity) {

        ?>

        <!--   <a href="<?php // Yii::getAlias('@web');?>/site/gallery?activity_name=<?php //$Categories['id'];?>"> -->
        <a href="<?php Yii::getAlias('@web');?>/site/gallery/?id=<?php echo $activity['id']; ?>">
            <br><h5 class="text-center"><?php echo $activity['activity_name']; ?></h5>
        </a>
    <?php
        }
    ?>
    </div>
    <div class="col-lg-10 col-md-10 col-sm-12 col-xs-12">
        <header class="section-header">
            <div>
                <h3 class="text-center">
                    <?php echo $model['activity_name'] ?>
                </h3>
                <h5 class="text-center">
                    <?php echo $model['activity_description'] ?>
                </h5>
                <br>
            </div>
        </header>
        <div id="grid-container" class="cbp-l-grid-projects">
            <ul>
                <?php foreach ($gallery as $galleries) {?>

                <div class="cbp-item" style="width:320px;">
                    <a href="<?php echo Yii::getAlias('@web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo $galleries['image']; ?>" data-lightbox="portfolio" data-title="<?php echo $galleries['title']; ?>"  title="Preview" class="cbp-lightbox">
                        <picture>
                            <source srcset ="<?php echo Yii::getAlias('@web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo basename($galleries['image_thumb'], '.jpg') . '.webp'; ?>" type="image/webp">
                            <source srcset ="<?php echo Yii::getAlias('@web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo $galleries['image_thumb']; ?>" type="image/jpeg">
                            <img src="<?php echo Yii::getAlias('@web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo $galleries['image_thumb']; ?>" style ="width:100%;" class="img-fluid" alt="">
                        </picture>
                    </a>
                </div>
                <?php

                    }
                ?>
            </ul>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

我想我已经用下面的代码解决了。

public function actionGallery($id=null) {
        //$this->layout = "@app/themes/frontend/main";
                $activities = Activities::find()->where(['status' => 1])->orderBy(['id'=>SORT_DESC])->all();
                $model = Activities::find()->where(['id' => $id])->one();

                $latestActivity = Activities::find()->max('id');

                if($id==null){
                    $gallery = Gallery::find()->where(['status' => 'active','activity_name'=>$latestActivity])->all(); 
                }else{
                    $gallery = Gallery::find()->where(['status' => 'active','activity_name'=>$id])->all();                    
                }

                return $this->render('gallery', ['gallery' => $gallery,'activities'=>$activities,'model'=>$model]);

    }