Doctrine orm 如何使用可嵌入性原则

Doctrine orm 如何使用可嵌入性原则,doctrine-orm,embeddable,Doctrine Orm,Embeddable,我正在尝试在Symfony 2项目中使用可嵌入的原则 我有一个类Purchase,其中我有一个price字段,该字段是可嵌入的: /** * Products * * @ORM\Table(name="purchases") * @ORM\Entity */ class Purchase { /** * * @ORM\Embedded(class="AppBundle\Entity\Embeddable\PriceEmbeddable") */

我正在尝试在Symfony 2项目中使用可嵌入的原则

我有一个类
Purchase
,其中我有一个
price
字段,该字段是可嵌入的:

/**
 * Products
 *
 * @ORM\Table(name="purchases")
 * @ORM\Entity
 */
class Purchase
{
    /**
     *
     * @ORM\Embedded(class="AppBundle\Entity\Embeddable\PriceEmbeddable")
     */
    private $price;

    /**
     * Set price
     *
     * @param MoneyInterface $price
     * @return $this
     */
    public function setPrice(MoneyInterface $price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return MoneyInterface|float
     */
    public function getPrice()
    {
        return $this->price;
    }

}
因为价格需要一种货币才能完成,所以我有一个可嵌入类来存储这两个值:

/**
 * @ORM\Embeddable
 */
class PriceEmbeddable
{
    /** @ORM\Column(type = "integer") */
    private $amount;

    /** @ORM\Column(type = "string") */
    private $currency;
}
现在,数据库中的模式已正确创建,但是,当我持久化
购买
实体时,会出现以下错误:

SQLSTATE[23000]:完整性约束冲突:1048列 “价格金额”不能为空

我相信这一点:我还不了解这种机制是如何工作的

如何从“真实”实体(
Purchase
)设置和获取值

我将值作为
Money
对象()传递给
Purchase
实体中的方法
setPrice()
,但是,如何将该值拆分为两个属性
amount
currency
并在可嵌入类中设置?

由于执行
var\u dump
(使用的
dump()
函数),我以正确的方式设置了实体:

PurchaseListener.php on line 58:
Purchase {#1795 ▼
  ...
  -price: Money {#1000 ▼
    -amount: 86
    -currency: Currency {#925 ▼
      -currencyCode: "EUR"
    }
  }
}
但是这些值没有在可嵌入的中设置,我不明白为什么

我还尝试在嵌入式类中硬编码值,但无论如何都不起作用,而且我也不明白为什么:

/**
 * @ORM\Embeddable
 */
class PriceEmbeddable
{
    /** @ORM\Column(type = "integer") */
    private $amount;

    /** @ORM\Column(type = "string") */
    private $currency;

    public function __construct($value)
    {
        $this->currency = 'EUR';
        $this->amount = 90;
    }

    public function setAmount($amount)
    {
        $this->amount = $amount = 90;
    }

    public function setCurrency($currency)
    {
        $this->currency = $currency = 'EUR';
    }

    public function getAmount()
    {
        return $this->amount;
    }

    public function getCurrency()
    {
        return $this->currency;
    }
}

这是一个简单的解决方案:

/**
 * Products
 *
 * @ORM\Table(name="purchases")
 * @ORM\Entity
 */
class Purchase
{
    /**
     *
     * @ORM\Embedded(class="AppBundle\Entity\Embeddable\PriceEmbeddable")
     */
    private $price;

    /**
     * Set price
     *
     * @param PriceEmbeddable $price
     * @return $this
     */
    public function setPrice(PriceEmbeddable $price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return PriceEmbeddable
     */
    public function getPrice()
    {
        return $this->price;
    }

}

您必须在
购买
构造函数中显式实例化
priceembeddeble

class Purchase
{
  ...

  function __construct() {
      $this->price = new PriceEmbeddable();
  }

  ...

}