0

我不知道这个问题是否有效,但我专门针对 Symfony 提出这个问题。

在 Sylius-sandbox 中:上传产品图片似乎不起作用。在演示站点和本地安装上进行了尝试。

当图像上传到产品时,什么都不会发生。显示默认占位符,上传目录中不显示任何内容。

也不创建上传目录。手动创建它似乎没有效果。

我在 symfony2.1 新安装中使用了https://github.com/Sylius/Sylius-Sandbox代码。我正在使用 App 和 Sylius-sandbox 使用 bin。

任何人创建或使用 Sylius-sandbox 帮助我...

产品.php

<?php

namespace Sylius\Sandbox\Bundle\AssortmentBundle\Entity;

use Sylius\Bundle\AssortmentBundle\Entity\CustomizableProduct as BaseProduct;
use Sylius\Bundle\CategorizerBundle\Model\CategoryInterface;
use Sylius\Bundle\InventoryBundle\Model\StockableInterface;
use Symfony\Component\Validator\Constraints as Assert;

class Product extends BaseProduct implements StockableInterface
{
const VARIANT_PICKING_CHOICE = 0;
const VARIANT_PICKING_MATCH  = 1;

/**
 * Product category.
 *
 * @Assert\NotBlank
 *
 * @var CategoryInterface
 */
protected $category;

/**
 * Variant picking mode.
 * Whether to display a choice form with all variants or match variant for
 * given options.
 *
 * @var integer
 */
protected $variantPickingMode;

/**
 * Image path.
 *
 * @var string
 */
protected $imagePath;

/**
 * Image upload.
 *
 * @Assert\File(maxSize="512k")
 * @Assert\Image
 */
public $image;

/**
 * Set default variant picking mode.
 */
public function __construct()
{
    parent::__construct();

    $this->variantPickingMode = self::VARIANT_PICKING_CHOICE;
}

/**
 * Get category.
 *
 * @return CategoryInterface
 */
public function getCategory()
{
    return $this->category;
}

/**
 * Set category.
 *
 * @param CategoryInterface $category
 */
public function setCategory(CategoryInterface $category)
{
    $this->category = $category;
}

public function getVariantPickingMode()
{
    return $this->variantPickingMode;
}

public function setVariantPickingMode($variantPickingMode)
{
    if (!in_array($variantPickingMode, array(self::VARIANT_PICKING_CHOICE, self::VARIANT_PICKING_MATCH))) {
        throw new \InvalidArgumentException('Wrong variant picking mode supplied');
    }

    $this->variantPickingMode = $variantPickingMode;
}

public function isVariantPickingModeChoice()
{
    return self::VARIANT_PICKING_CHOICE === $this->variantPickingMode;
}

/**
 * This is a proxy method to access master variant price.
 * Because if there are no options/variants defined, the master variant is
 * the project.
 *
 * @return float
 */
public function getPrice()
{
    return $this->masterVariant->getPrice();
}

/**
 * Implementation of stockable interface.
 * Proxy to use master variant for managing inventory status.
 *
 * {@inheritdoc}
 */
public function getStockableId()
{
    return $this->masterVariant->getStockableId();
}

/**
 * {@inheritdoc}
 */
public function isInStock()
{
    return $this->masterVariant->inStock();
}

/**
 * {@inheritdoc}
 */
public function getOnHand()
{
    return $this->masterVariant->getOnHand();
}

/**
 * {@inheritdoc}
 */
public function setOnHand($onHand)
{
    $this->masterVariant->setOnHand($onHand);
}

public function getImagePath()
{
    return $this->imagePath;
}

public function setImagePath($imagePath)
{
    $this->imagePath = $imagePath;
}

public function getAbsoluteImagePath()
{
    return null === $this->getImagePath() ? null : $this->getImageUploadRootDir().'/'.$this->getImagePath();
}

public function getImageWebPath()
{
    return null === $this->getImagePath() ? null : $this->getImageUploadDir().'/'.$this->getImagePath();
}

public function getImageUploadDir()
{
    return 'uploads/images';
}

public function hasImage()
{
    return null !== $this->getImagePath();
}

public function saveImage()
{
    if (null === $this->image) {

        return;
    }

    $this->setImagePath(uniqid().'.'.$this->image->guessExtension());
    $this->image->move($this->getImageUploadRootDir(), $this->getImagePath());
}

public function deleteImage()
{
    if ($file = $this->getAbsoluteImagePath()) {
        unlink($file);
    }
}

protected function getImageUploadRootDir()
{
    return __DIR__.'/../../../../web/'.$this->getImageUploadDir();
}

static public function getVariantPickingModeChoices()
{
    return array(
        self::VARIANT_PICKING_CHOICE => 'Display list of variants',
        self::VARIANT_PICKING_MATCH  => 'Display options'
    );
}

/**
 * Get comment thread ID.
 *
 * @return string
 */
public function getCommentThreadId()
{
    return 'assortment_product_'.$this->getId();
}
}
4

1 回答 1

0

我也使用了 sylius 沙箱应用程序作为指南,如果不用作指南可能会导致更多混乱,请尝试制作自己的代码并使用沙箱作为参考。

2分...

  1. 我相信 Sylius-Sandbox 已被弃用(改用Sylius/Sylius
  2. 要实现图像管理,可能有 2 个选项: 1- 使用 sylius 的内置图像管理,它实现每个变体的图像。2-构建您自己的实现,类似于您在产品实体代码中的实现。

在我的实现中,我认为使用 Product 实体中的 OneToMany 关系与 Image 实体更简单,显然这取决于您的要求。我为 Image 实体使用了symfony 文档

于 2013-08-14T18:29:44.513 回答