Symfony集合不保存引用ID,也不能删除元素

时间:2017-07-26 13:49:08

标签: symfony doctrine sylius

我添加了一个带有关系附件的附件附件 - Product ManyToOne。架构是正确创建的,问题是,当我保存产品时,product_id没有设置在附件表中,我通过使用DataTransformer解决了我手动添加产品参考的问题。另一个问题是我无法从CollectionType中删除附件。

ProductAttachment class:

  <?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 个答案:

答案 0 :(得分:1)

根据Symfony Collection Of Form文档,您应该保存&#34; Inverse&#34;侧

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

也无需检查$ attachment是否为null(您将变量转换为Class类型)