Question

I don't know if this question is valid or not, but I'm asking this specifically for Symfony.

In Sylius-sandbox: Uploading a product image does not seem to work. Tried on the demo site and on a local installation.

When an image is uploaded to a product, nothing happens. The default placeholder is shown and nothing appears in the upload directory.

The upload directory is also not created. Manually creating it seems to have no effect.

I have use https://github.com/Sylius/Sylius-Sandbox code in symfony2.1 new installation. I am use App and Sylius-sandbox use bin.

Any one create or use Sylius-sandbox help me...

product.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();
}
}
Was it helpful?

Solution

I used sylius sandbox aplication as a guide too, if not used as a guide it may cause more confusion, try to make your own code all over and use the sandbox as a reference.

2 points...

  1. I believe Sylius-Sandbox is deprecated (use Sylius/Sylius instead)
  2. To implement image management, there may be 2 options: 1- use the built-in image management from sylius which implements an image per variant. 2- Build your own implementation similar to what you have in your product entity code.

On my implementation I thought it was simpler to use a OneToMany relationship from the Product entity with an Image entity, obviously it depends on your requirements. I used symfony documentation for the Image entity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top