cakePHP 3.0 - 在同一视图中以不同方式显示图像 - querybuilder

cakePHP 3.0 - displaying images differently in the same view - querybuilder

我有一个项目 table 和一个图像 table。一个项目有很多图像,一个图像属于一个项目。在图像 table 中,我有一个名为 image_dimensions 的字段。我想要实现的是获得 foreach 循环,但是每个 image_dimensions.

的循环都需要不同

我可以这样显示所有图片:

<?php foreach ($project->images as $images): ?>

例如数据库中有5张图片。 3 张尺寸为 2x2 的图像和 2 张尺寸为 3x3 的图像。所以在下面的代码中,它将给我 3 个 div 的图像和 2 个空的 div。那些空的 div 不应该在那里。

每个图像维度也需要不同的是 class 的 div。有时我想使用 col-lg-6,有时我想使用 col-lg-12,...

<?php foreach ($project->images as $images): ?>
    <div class="col-lg-6">
       <?php
         if($images->image_path && $images->image_dimensions == '2x2')
         echo $this->Html->image($images->image_path, array(
        'class' => 'img-responsive center-block project-images'));
       ?>
    </div>
<?php endforeach; ?>

我还阅读了有关 cakephp 中的查询生成器的信息。我想我需要用它来解决我的问题。但似乎无法使查询正常工作。

$project = $this->Projects->find();
$project->matching('Images', function($q){
   return $q->where(['Images.image_dimensions' => '2x2']);
}

我想要实现的是显示所有图像,但是 2x2 尺寸的图像应该与 3x3 尺寸的图像显示不同。当然,所有图像都在同一视图中。因此图像的 class 和图像周围 div 的 class 对于 2x2 和 3x3 图像尺寸应该不同。 是否可以在 foreach 循环中添加条件?

我的 Projectscontroller 中的视图动作

public function view($id = null)
{
    $project = $this->Projects->get($id, [
        'contain' => ['Categories', 'Users', 'Tags', 'Images'],
    ]);

    $this->set('project', $project);
    $this->set('_serialize', ['project']);
}

解决方案

<?php if($images->image_dimensions == '2x2')
         echo $this->Html->div(array('class' => 'col-lg-6')
      );
      elseif($images->image_dimensions == '3x3')
         echo $this->Html->div(array('class' => 'col-lg-12')
      );
?>