【问题标题】:How to use Doctrine Embeddables如何使用 Doctrine Embeddables
【发布时间】:2015-11-23 07:16:36
【问题描述】:

我正在尝试在 Symfony 2 项目中使用 Doctrine 嵌入式。

我有一个 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;
}

现在,数据库中的架构已正确创建,但是,当我持久化 Purchase 实体时,出现以下错误:

SQLSTATE[23000]:违反完整性约束:1048 列 'price_amount' 不能为空

我相信它:我还不明白这个机制是如何工作的。

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

我将值作为Money 对象(a value object I use)传递给Purchaseentity 中的方法setPrice(),但是,如何将该值拆分为两个属性amount 和@ 987654333@ 并设置在可嵌入类中?

因为执行var_dump(使用VarDumperdump() 函数)我得到了正确的实体集:

PurchaseListener.php on line 58:
Purchase {#1795 ▼
  ...
  -price: Money {#1000 ▼
    -amount: 86
    -currency: Currency {#925 ▼
      -currencyCode: "EUR"
    }
  }
}

但是这些值没有在 Embeddable 中设置,我不明白为什么......

我也尝试对嵌入类中的值进行硬编码,但无论如何它不起作用,而且我也不明白为什么:

/**
 * @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;
    }
}

【问题讨论】:

    标签: doctrine-orm embeddable


    【解决方案1】:

    这是简单的解决方案:

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

    【讨论】:

      【解决方案2】:

      您必须在 Purchase 构造函数中显式实例化 PriceEmbeddable

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

      【讨论】:

        猜你喜欢
        • 2014-08-05
        • 2015-02-22
        • 2015-07-04
        • 2011-10-07
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 2011-05-13
        • 2012-01-21
        相关资源
        最近更新 更多