将实体复制到“不同类”?

问题描述 投票:0回答:1

我有一个customer和一个order实体。 (学说实体)

两者都有一个address相关实体,实现相同的AddressInterface

当客户下订单时,我想将客户的地址复制到订单的地址。 (我需要一份副本,因为我不希望在客户地址发生变化时更改订单地址)

所以我当然可以这样做:

$orderAddress = new OrderInvoiceAddress();
$orderAddress->setLastName( $customerAddress->getLastName() );
...
...

但我想知道是否有更优雅的方式?

php symfony doctrine-orm
1个回答
0
投票

使用静态方法:

class OrderAddress {

    static function CreateFromCustomerAddress(CustomerAddress $customerAddress)
    {
        $orderAddress=new self();
        $orderAddress->setFirstName($customerAddress->getFirstName());

        return $orderAddress;
    }

    private $firstName;

用法:

$orderAddress=OrderAddress::CreateFromCustomerAddress($customerAddress);
© www.soinside.com 2019 - 2024. All rights reserved.