) | ) | ||||
); | ); | ||||
$formBuilder->add('products', CollectionType::class, array( | |||||
'entry_type'=> ProductType::class, | |||||
'allow_add'=>true, | |||||
'prototype'=> true | |||||
) | |||||
); | |||||
return $formBuilder; | return $formBuilder; | ||||
} | } | ||||
namespace Lc\ShopBundle\Form; | namespace Lc\ShopBundle\Form; | ||||
use CKSource\Bundle\CKFinderBundle\Form\Type\CKFinderFileChooserType; | use CKSource\Bundle\CKFinderBundle\Form\Type\CKFinderFileChooserType; | ||||
use Doctrine\ORM\EntityManagerInterface; | |||||
use FOS\CKEditorBundle\Form\Type\CKEditorType; | use FOS\CKEditorBundle\Form\Type\CKEditorType; | ||||
use Lc\ShopBundle\Context\ProductFamilyInterface; | use Lc\ShopBundle\Context\ProductFamilyInterface; | ||||
use Lc\ShopBundle\Context\ProductInterface; | |||||
use Lc\ShopBundle\Context\TaxRateInterface; | use Lc\ShopBundle\Context\TaxRateInterface; | ||||
use function PHPSTORM_META\type; | use function PHPSTORM_META\type; | ||||
use Symfony\Component\Form\AbstractType; | use Symfony\Component\Form\AbstractType; | ||||
class ProductType extends AbstractType | class ProductType extends AbstractType | ||||
{ | { | ||||
protected $em; | |||||
public function __construct(EntityManagerInterface $entityManager) | |||||
{ | |||||
$this->em = $entityManager; | |||||
} | |||||
public function buildForm(FormBuilderInterface $builder, array $options) | public function buildForm(FormBuilderInterface $builder, array $options) | ||||
{ | { | ||||
$builder | $builder | ||||
->add('title') | ->add('title') | ||||
->add('image', CKFinderFileChooserType::class, array( | |||||
'block_name' => 'ckfinder_file_chooser_widget' | |||||
)) | |||||
->add('description', CKEditorType::class) | |||||
->add('supplier') | |||||
->add('productCategories'); | |||||
->add('step') | |||||
->add('weight') | |||||
->add('step') | |||||
->add('price'); | |||||
} | } | ||||
public function configureOptions(OptionsResolver $resolver) | public function configureOptions(OptionsResolver $resolver) | ||||
{ | { | ||||
$resolver->setDefaults([ | $resolver->setDefaults([ | ||||
'data_class' => ProductFamilyInterface::class, | |||||
'data_class' => $this->em->getClassMetadata(ProductInterface::class)->getName(), | |||||
]); | ]); | ||||
} | } | ||||
} | } |
/** | /** | ||||
* @ORM\MappedSuperclass() | * @ORM\MappedSuperclass() | ||||
*/ | */ | ||||
abstract class Product extends AbstractDocumentEntity | |||||
abstract class Product extends AbstractEntity | |||||
{ | { | ||||
/** | /** | ||||
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\ProductFamilyInterface", inversedBy="products") | * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\ProductFamilyInterface", inversedBy="products") | ||||
*/ | */ | ||||
protected $productFamily; | protected $productFamily; | ||||
/** | |||||
* @ORM\Column(type="string", length=255) | |||||
*/ | |||||
protected $title; | |||||
/** | /** | ||||
* @ORM\Column(type="float", nullable=true) | * @ORM\Column(type="float", nullable=true) | ||||
*/ | */ | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getTitle(): ?string | |||||
{ | |||||
return $this->title; | |||||
} | |||||
public function setTitle(string $title): self | |||||
{ | |||||
$this->title = $title; | |||||
return $this; | |||||
} | |||||
} | } |
use Doctrine\Common\Collections\ArrayCollection; | use Doctrine\Common\Collections\ArrayCollection; | ||||
use Doctrine\Common\Collections\Collection; | use Doctrine\Common\Collections\Collection; | ||||
use Doctrine\ORM\Mapping as ORM; | use Doctrine\ORM\Mapping as ORM; | ||||
use Lc\ShopBundle\Context\ProductInterface; | |||||
/** | /** | ||||
* @ORM\MappedSuperclass() | * @ORM\MappedSuperclass() | ||||
*/ | */ | ||||
protected $productCategories; | protected $productCategories; | ||||
/** | |||||
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="productFamily", orphanRemoval=true) | |||||
*/ | |||||
private $products; | |||||
/** | /** | ||||
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\TaxRateInterface") | * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\TaxRateInterface") | ||||
* @ORM\JoinColumn(nullable=true) | * @ORM\JoinColumn(nullable=true) | ||||
public function __construct() | public function __construct() | ||||
{ | { | ||||
$this->productCategories = new ArrayCollection(); | $this->productCategories = new ArrayCollection(); | ||||
$this->products = new ArrayCollection(); | |||||
} | } | ||||
public function getMerchant(): ?Merchant | public function getMerchant(): ?Merchant | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | |||||
* @return Collection|ProductInterface[] | |||||
*/ | |||||
public function getProducts(): Collection | |||||
{ | |||||
return $this->products; | |||||
} | |||||
public function addProduct(ProductInterface $product): self | |||||
{ | |||||
if (!$this->products->contains($product)) { | |||||
$this->products[] = $product; | |||||
$product->setProductFamily($this); | |||||
} | |||||
return $this; | |||||
} | |||||
public function removeProduct(ProductInterface $product): self | |||||
{ | |||||
if ($this->products->contains($product)) { | |||||
$this->products->removeElement($product); | |||||
// set the owning side to null (unless already changed) | |||||
if ($product->getProductFamily() === $this) { | |||||
$product->setProductFamily(null); | |||||
} | |||||
} | |||||
return $this; | |||||
} | |||||
/** | /** | ||||
* @return Collection|ProductCategory[] | * @return Collection|ProductCategory[] | ||||
*/ | */ |