12

我需要将一些不可变字段移动到单独的类中,但我真的不想使用“join”,因为我每次都需要将所有数据放在一起。

有什么方法可以将一些实体属性作为映射到同一个表的类?

就像是:

/**
 * @ORM\Entity
 */
class User {
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    ...

    /**
     * @var Address
     * @ORM\... ??
     */
    protected $address
}

/**
 * @ORM\ValueObject ??
 */
class Address {
    /**
     * @var string
     * @ORM\Column(type="string", name="address_zipcode", length=12)
     */
    protected $zipcode;

    /**
     * @var string
     * @ORM\Column(type="string", name="address_country_iso", length=3)
     */
    protected $countryIso;
    ...
}

表结构将是:

CREATE TABLE User (
    `id` INT(11) NOT NULL auto_increment,
    `address_zipcode` VARCHAR(12) NOT NULL,
    `address_country_iso` VARCHAR(3) NOT NULL,
    PRIMARY KEY (`id`)
);
4

3 回答 3

1

您要问的是所谓的值对象。

Jira DDC-93中有一个未解决的问题来添加支持。它当前在 2.5 版中标记为已解决,该版本刚刚在 Beta 中发布。

于 2014-02-28T07:23:57.453 回答
0

就像你说的那样。

将 @PreUpdate 和 @PostLoad 挂钩添加到User.

/**
 * @ORM\Entity
 */
class User {
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    ...

    /**
     * @var Address
     * (NOTE: no @ORM annotation here)
     */
    protected $address

    /**
     * @var string
     * @ORM\Column(type="string")
     */
    protected $addressZipCode;

    /**
     * @var string
     * @ORM\Column(type="string")
     */
    protected $addressCountryIso;

    public function setAddress(Address $address)
    {
        $this->address = $address;
    }

    /**
     * @ORM\PreUpdate
     *
     * set $addressZipCode and $addressCountryInfo when this object is to
     * save so that doctrine can easily save these scalar values
     */
    public function extractAddress()
    {
        $this->addressZipCode = $this->address->getZipCode();
        $this->addressCountryIso = $this->address->getAddressCountryIso();
    }

    /**
     * @ORM\PostLoad
     *
     * When the row is hydrated into this class,
     * $address is not set because that isn't mapped.
     * so simply, map it manually
     */
    public function packAddress()
    {
        $this->address = new Address();
        $this->address->setZipCode($this->addressZipCode);
        $this->address->setCountryIs($this->addressCountryIso);
    }
}
于 2014-01-20T02:25:53.063 回答
0

如果您想在没有连接的情况下存储对象:

/**
 * @ORM\Column(name="adress", type="object")
 */

它将自动序列化/反序列化到文本字段

http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html

并添加您要存储的类型的 setter

public function setAdress(Address $adress)
{
    $this->adress = $adress;

    return $this;
}

Adress 类不需要任何 @ORM 注释

于 2013-11-06T16:35:10.980 回答