J'ai ajouté des pièces jointes au produit avec des liens Attachment - Product ManyToOne. Le schéma est créé correctement, le problème est que lorsque je sauvegarde le produit, product_id n'est pas défini dans la table des pièces jointes, ce que j'ai résolu en utilisant DataTransformer où j'ajoute manuellement la référence du produit. Un autre problème est que je ne peux pas supprimer la pièce jointe de CollectionType.La collection Symfony n'enregistre pas l'ID de référence et ne peut pas supprimer l'élément.
classe ProductAttachment:
<?php
namespace AppBundle\Entity;
/**
* ProductAttachment
*/
class ProductAttachment
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $filename;
private $filepath;
/**
* @var string
*/
private $filetype;
private $product;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getFilepath() {
return $this->filepath;
}
/**
* @param mixed $filepath
*/
public function setFilepath($filepath) {
$this->filepath = $filepath;
}
/**
* @return mixed
*/
public function getProduct() {
return $this->product;
}
/**
* @param mixed $product
*/
public function setProduct($product) {
$this->product = $product;
}
/**
* Set filename
*
* @param string $filename
*
* @return ProductAttachment
*/
public function setFilename($filename)
{
$this->filename = $filename;
return $this;
}
/**
* Get filename
*
* @return string
*/
public function getFilename()
{
return $this->filename;
}
/**
* Set filetype
*
* @param string $filetype
*
* @return ProductAttachment
*/
public function setFiletype($filetype)
{
$this->filetype = $filetype;
return $this;
}
/**
* Get filetype
*
* @return string
*/
public function getFiletype()
{
return $this->filetype;
}
}
est ici Classe de produit:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Sylius\Component\Core\Model\Product as BaseProduct;
use Sylius\Component\Product\Model\ProductTranslation;
use Sylius\Component\Resource\Model\TranslatableTrait;
class Product extends BaseProduct {
private $nid;
private $attachments;
use TranslatableTrait {
__construct as private initializeTranslationsCollection;
}
public function __construct()
{
parent::__construct();
$this->initializeTranslationsCollection();
$this->attachments = new ArrayCollection();
}
/**
* @return mixed
*/
public function getAttachments() {
return $this->attachments;
}
/**
* @param mixed $attachments
*/
public function setAttachments($attachments) {
$this->attachments = $attachments;
}
public function addAtachment(ProductAttachment $attachment) {
if($attachment != null) {
$this->attachments->add($attachment);
}
}
public function removeAttachment(ProductAttachment $attachment) {
$this->attachments->remove($attachment);
}
/**
* @return mixed
*/
public function getNid() {
return $this->nid;
}
/**
* @param mixed $nid
*/
public function setNid($nid) {
$this->nid = $nid;
}
public function createTranslation() {
return new ProductTranslation();
}
}
définitions Doctrine:
Produit:
AppBundle\Entity\Product:
type: entity
table: sylius_product
fields:
nid:
type: integer
nullable: true
oneToMany:
attachments:
targetEntity: ProductAttachment
mappedBy: product
cascade: ["persist","remove"]
orphanRemoval: true
ProductAttachment:
AppBundle\Entity\ProductAttachment:
type: entity
table: sylius_product_attachment
repositoryClass: AppBundle\Repository\ProductAttachmentRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
filename:
type: string
length: 255
filetype:
type: string
length: 255
filepath:
type: string
length: 255
manyToOne:
product:
targetEntity: product
joinTable:
name: sylius_product
joinColumns:
product_id:
referencedColumnName: id
inverseJoinColumns:
attachment_id:
referencedColumnName: id
lifecycleCallbacks: { }
Merci. Je devrais lire la documentation en premier. Je n'avais aucune idée de la façon dont les "additionneurs" et les "dissolvants" fonctionnent et qu'ils sont déclenchés après la sauvegarde de la forme. – peter21