0

我正在使用 Propel 来检索产品列表,其中一些产品具有与它们相关联的图像,该图像位于上传文件夹中并且基于图像 id(例如/uploads/products/product_12.jpg)将是具有 id 的产品的图像12.

由于此图像记录未作为列存储在数据库中,因此我不确定如何访问图像而无需file_exists()在视图中进行检查。

是否可以有一个类似的方法来$product->getImage() 返回图像或返回一个空白的“图像不存在”图像,或者更好的是,创建一个image与上面相同并且可以通过$product->image.

4

1 回答 1

1

当然。

在您的模型类(不是 Peer 类,另一个)中,创建此方法getImage()基本上可以:

public function getImage()
{
  $filepath = '/uploads/products/product_'.$this->getId().'.jpg';
  if (!file_exists($filepath))
  {
    // image doesn't exist, return false or an empty default image
    return false;
  }

  return $filepath;
}
于 2012-10-21T13:06:14.667 回答