1

我怎么解决这个问题。一个实体类聚合一个对象。它的值应该存储在数据库中。我有以下实体:

<?php

class Price
{
    private $_amount;

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

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

/**
 * Class Product
 *
 * @Entity
 * @Table(name="product", options={"engine" = "NDBCLUSTER"})
 */
class Product
{
    /**
     * @Id
     * @GeneratedValue
     * @Column(name="id", type="integer")
     * @var int
     */
    private $_id;

    /**
     * @Column(name="price", type="decimal", precision=10, scale=2)
     * @var Price
     */
    private $_price;
}

?>

如果我想在数据库中存储价格,我该怎么做?这显然是行不通的:

<?php

$price = new Price();
$price->setAmount(19.99);

$product = new Product();
$product->setPrice($price);

$em->persist($product);
$em->flush();

?>

我必须实现新的映射类型吗?

4

3 回答 3

1

您可以使用教义Embeddables

像这样将您的价格声明为可嵌入的:

/**
 * @Embeddable 
 */
class Price
{
    /** @Column(type = "decimal") */
    private $_amount;

    ...

然后使用它:

class Product
{
    /** @ORM\Embedded(class="Price") */
    private $price;

    ...
于 2017-09-08T10:44:30.237 回答
0

您可以定义自定义映射类型

于 2013-10-04T14:32:41.663 回答
0

您应该在学说中创建自定义类型,例如:

declare(strict_types = 1);

namespace App\Infrastructure\Persistence\Doctrine\Type;

use App\Domain\Model\OrderStatus;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\TextType;

final class StatusDoctrineType extends TextType
{
    /**
     * @return string
     */
    public function getName()
    {
        return 'status_doctrine_type';
    }

    /**
     * @param                  $value
     * @param AbstractPlatform $platform
     *
     * @return OrderStatus
     * @throws ConversionException
     */
    public function convertToPhpValue($value, AbstractPlatform $platform): OrderStatus
    {
        if (null === $value) {
            throw ConversionException::conversionFailed($value, $this->getName());
        }

        return new OrderStatus($value);
    }

    /**
     * @param mixed            $value
     * @param AbstractPlatform $platform
     *
     * @return null|string
     * @throws ConversionException
     */
    public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
    {
        if (null === $value) {
            throw ConversionException::conversionFailed($value, $this->getName());
        }
        /** @var OrderStatus $value */
        if (!is_a($value, OrderStatus::class)) {
            throw ConversionException::conversionFailed($value, $this->getName());
        }

        return $value->currentStatus();
    }
} 

在你的实体中是这样的:

// ...
/**
 * Order constructor.
 *
 * @param Route    $route
 * @param OrderId  $orderId
 * @param Distance $distance
 */
public function __construct(Route $route, OrderId $orderId, Distance $distance)
{
    $this->route = $route;
    $this->id = $orderId;
    $this->distance = $distance->spacing();

    // according to grasp Information Expert pattern.
    $this->status = new OrderStatus(OrderStatus::UNASSIGN);
}
//...
   /**
 * @return OrderStatus
 */
public function status(): OrderStatus
{
    return $this->status;
}
于 2018-11-14T13:32:33.777 回答