我不知道这个问题是否有效,但我是专门针对Symfony问的。
在Sylius-sandbox中: 上传产品图片似乎不起作用。试用了演示站点和本地安装。
将图像上传到产品时,没有任何反应。显示默认占位符,上传目录中不显示任何内容。
也没有创建上传目录。手动创建它似乎没有效果。
我在symfony2.1新安装中使用了https://github.com/Sylius/Sylius-Sandbox代码。 我使用App和Sylius-sandbox使用bin。
任何人创建或使用Sylius-sandbox都会帮助我......
<?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();
}
}
答案 0 :(得分:0)
我也使用了sylius sandbox aplication作为指南,如果不作为指南使用它可能会引起更多的混淆,尝试自己编写代码并使用沙箱作为参考。
2分......
在我的实现中,我认为使用Product实体与Image实体的OneToMany关系更简单,显然这取决于您的要求。我使用symfony documentation作为Image实体。