Doctrine2将不可变字段移动到单独的类中

ikh*_*lev 12 php mysql doctrine-orm

我需要将一些不可变字段移动到单独的类中,但我真的不想使用"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;
    ...
}
Run Code Online (Sandbox Code Playgroud)

表结构将是:

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`)
);
Run Code Online (Sandbox Code Playgroud)

ikh*_*lev 0

Doctrine 在版本中引入了值对象的概念2.x

新属性/注释的名称是EmbeddableEmbedded。第一个用于标记值对象类,第二个用于将值对象实际注入到父实体中。

这是我原来问题的更新代码片段:

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

    /** @ORM\Embedded(class="Address", columnPrefix="address") */
    protected $address
}

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

    /** @ORM\Column(type="string", name="address_country_iso", length=3) */
    protected $countryIso;
    ...
}
Run Code Online (Sandbox Code Playgroud)

资源: