我正在用 cakephp3.7 构建一个电子商务网站,对于卖家发布他们的产品,他们需要上传要销售的文章图片,并且通过带有 Blob 列的表单。图像从视图上传得很好,但在数据库中,图像的真实名称没有出现,取而代之的是,在那个地方,所有图像的名称都是所有文章的“Blob”。现在的问题是,当我尝试从数据库中检索文章的图像(仅通过它的名称知道)到控制器以便将其发送到网站的前端时,它找不到图像,我我被严重卡住了!!!
但是当我到达 webroot/img 文件夹时,我找到了上传的图像,当我在前端视图中手动复制/粘贴名称时,图像会与文章的其他属性一起出现在前端。
这是应该从数据库中查找文章及其图像的控制器代码,要寻址的表是表“annonces”,存储图像的表的字段是“piece_jointe”:
public function articles()
{
$this->viewBuilder()->layout('react2019');
$annonces = $this->Annonces->find()
->where(['Annonces.subcategorie_id'=>1])
->contain(['Comptes', 'Subcategories'])
->all();
$this->set(compact('annonces'));
}
这是将发布的文章转发到前端时的文章视图,我现在无法检索的字段是“piece_jointe”:
<?php foreach ($annonces as $annonce): ?>
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<a href="#"><?= $this->Html->image($annonce->piece_jointe, array('height'=>90, 'width'=>90)); ?></a>
<div class="card-body">
<h4 class="card-title">
<a href="#"><?= h($annonce->annonce_titre) ?></a>
</h4>
<h5><?= h($annonce->prix_produit) ?></h5>
<p class="card-text"><?= h($annonce->annonce_description) ?></p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
<?php endforeach; ?>
我想知道从 mysql 数据库中检索 Blob 类型“piece_jointe”的名称并在 cakephp 语法中使用它的方法:
<?= $this->Html->image($annonce->piece_jointe, array('height'=>90, '
width'=>90)); ?>
因为当我将 $annonce->piece_jointe 变量替换为它的真实名称“macbook”时,这可以正常工作,例如:
<?= $this->Html->image('macbook', array('height'=>90, 'width'=>90)); ?>