2017-07-26 49 views
1

我已經添加了具有關係附件 - 產品ManyToOne的產品附件。模式正確創建,問題是,當我保存產品時,product_id沒有在附件表中設置,我通過使用DataTransformer解決了手動添加產品參考的問題。另一個問題是我無法從CollectionType中刪除附件。Symfony集合不保存參考ID並且不能刪除元素

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; 
    } 
    } 

下面是產品類:

<?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(); 
    } 


} 

主義的定義:

產品:

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: { } 

回答

1

根據Symfony的Collection Of Form文檔,你應該保存「反」面

public function addAtachment(ProductAttachment $attachment) 
    { 
     $attachment->setProduct($this); 
     $this->attachments->add($attachment); 
    } 

也沒有必要檢查是否$附件爲空(你是鑄造你的變量類型)

+0

謝謝。我應該先閱讀文檔。我不知道「加法器」和「刪除器」是如何工作的,它們是在表單被保存後觸發的。 – peter21