【问题标题】:Symfony / PHP - Entity inside other entity in Form Type classSymfony / PHP - 表单类型类中其他实体内的实体
【发布时间】:2016-03-20 10:57:57
【问题描述】:

我有以下问题。 Payment 实体表示付款类型。所以我可以有例如:“信用卡”收费 2 欧元,“PayPal”收费 1.50 欧元等等。

然后我有实体 OrderPaymentItem ,它存储电子商店订单中使用的支付类型。为什么?我需要为每个电子商店订单存储付款,因为当我更改付款费用时,它使用客户在完成订单时使用的费用......如果我将付款直接连接到订单实体而不是 OrderPaymentItem,它重新计算旧订单,这是错误的。当您更改费用时,它应该只在新订单中更改......但我不知道如何更正它。先看看这两个类:

第一个实体是支付

class Payment
{
    protected $id;
    protected $method;
    protected $fee;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set fee
     *
     * @param integer $fee
     * @return Payment
     */
    public function setFee($fee)
    {
        $this->fee = $fee;

        return $this;
    }

    /**
     * Get fee
     *
     * @return integer
     */
    public function getFee()
    {
        return $this->fee;
    }

    /**
     * Set method
     *
     * @param string $method
     * @return Payment
     */
    public function setMethod($method)
    {
        $this->method = $method;

        return $this;
    }

    /**
     * Get method
     *
     * @return string
     */
    public function getMethod()
    {
        return $this->method;
    }
}


AppBundle\Entity\Payment:
    type: entity
    table: payment
    id:
        id:
            type: integer
            generator:
                strategy: AUTO
    fields:
        method:
            column: method
            type: string
            nullable: false
            unique: true
            length: 64
        fee:
            column: fee
            type: integer
            nullable: false

第二个实体是OrderPaymentItem

class OrderPaymentItem
{
    protected $id;
    protected $method;
    protected $fee;
    protected $paymentId;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set fee
     *
     * @param integer $fee
     * @return Payment
     */
    public function setFee($fee)
    {
        $this->fee = $fee;

        return $this;
    }

    /**
     * Get fee
     *
     * @return integer
     */
    public function getFee()
    {
        return $this->fee;
    }

    /**
     * Set method
     *
     * @param string $method
     * @return Payment
     */
    public function setMethod($method)
    {
        $this->method = $method;

        return $this;
    }

    /**
     * Get method
     *
     * @return string
     */
    public function getMethod()
    {
        return $this->method;
    }

    /**
     * Set paymentId
     *
     * @param \AppBundle\Entity\Payment $paymentId
     * @return OrderDiscountItem
     */
    public function setPaymentId(\AppBundle\Entity\Payment $paymentId = null)
    {
        $this->paymentId = $paymentId;

        return $this;
    }

    /**
     * Get paymentId
     *
     * @return \AppBundle\Entity\Payment
     */
    public function getPaymentId()
    {
        return $this->paymentId;
    }
}


AppBundle\Entity\OrderPaymentItem:
        type: entity
        table: order_payment_item
        id:
            id:
                type: integer
                generator:
                    strategy: AUTO
        fields:
            method:
                column: method
                type: string
                nullable: false
                length: 64
            fee:
                column: fee
                type: integer
                nullable: false
        manyToOne:
            paymentId:
                targetEntity: AppBundle\Entity\Payment
                joinColumn:
                    name: payment_id
                    referencedColumnName: id
                    onDelete: SET NULL
                    nullable: true

最初我有以下篮子形式的表单生成器:

$builder->add('payment', 'entity', [
              'label' => 'Platba',
              'class' => 'AppBundle:Payment',
              'data_class' => 'AppBundle\Entity\Payment',
              'property' => 'method',
              'multiple' => false,
              'expanded' => true
]);

您可以看到这两个类几乎相同。只有 OrderPaymentItem 包含与付款的关系 - 不是必需的,但有利于向后兼容。

我现在需要更正它并使用 OrderPaymentItem 而不是 Payment。我需要使用付款列表,但将其保存为 OrderPaymentItem。

谁能帮帮我?

【问题讨论】:

    标签: php symfony doctrine symfony-forms symfony-2.7


    【解决方案1】:

    由于两个类几乎相同,所以可以使用Doctrine discriminator

    Take a look at this

    使用鉴别器映射时,例如,如果使用单表继承,您的“映射实体”将扩展基本实体。

    This should help you

    P.S : 我希望现在还不算太晚...

    【讨论】:

      猜你喜欢
      • 2016-03-21
      • 2016-06-26
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多