【问题标题】:Symfony 4 how to implement Doctrine XML ORM mappingSymfony 4 如何实现 Doctrine XML ORM 映射
【发布时间】:2019-03-28 11:56:59
【问题描述】:

Symfony 4 文档不清楚如何使用 XML orm 映射而不是注释。在官方文档中看不到如此重要部分的详细信息是相当令人沮丧的。

【问题讨论】:

    标签: doctrine symfony4


    【解决方案1】:

    想象一下YourDomain\Entity\Customer 域对象:

    <?php declare(strict_types=1);
    
    namespace YourDomain\Entity;
    
    class Customer
    {
        private $id;
        private $email;
        private $password;
    
        public function __construct(string $email)
        {
            $this->setEmail($email);
        }
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        public function getEmail(): string
        {
            return $this->email;
        }
    
        public function setEmail(string $email): void
        {
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                throw new \InvalidArgumentException('Not a valid e-mail address');
            }
    
            $this->email = $email;
        }
    
        public function getPassword(): ?string
        {
            return $this->password;
        }
    
        public function setPassword(?string $password): void
        {
            $this->password = $password;
        }
    }
    
    

    首先定义你自己的映射:

    orm:
        mappings:
            YourDomain\Entity:
                is_bundle: false
                type: xml
                // this is the location where xml files are located, mutatis mutandis
                dir: '%kernel.project_dir%/../src/Infrastructure/ORM/Mapping'
                prefix: 'YourDomain\Entity'
                alias: YourDomain
    

    文件名必须与模式[class_name].orm.xml 匹配,在您的情况下为Customer.orm.xml。如果你里面有子命名空间,例如。值对象YourDomain\Entity\ValueObject\Email,文件必须命名为ValueObject.Email.orm.xml

    示例映射:

    <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                       https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">
        <entity name="YourDomain\Entity\Customer" table="customer">
            <id name="id" type="integer" column="id">
                <generator strategy="AUTO"/>
            </id>
            <field name="email" type="email" unique="true"/>
            <field name="password" length="72"/>
        </entity>
    </doctrine-mapping>
    

    祝你好运。

    【讨论】:

    • 感谢 emix。创建 xml 映射后,您使用什么命令生成实体。例如,make:entity 或教义:generate:entities ?
    • 我从不这样做。我有“领域优先”的态度。一旦域模型完成并包含单元测试,我就会进行映射。
    • 你的意思是你手动创建实体文件?我来自 symfony 3,现在我对这部分没有任何想法。我找到了thinktocode.com/2018/04/19/doctrine-xml-mapping-or-annotations,但它也没有提到如何生成实体的细节。
    • 没错。为什么首先需要生成实体?您的实体将与往常一样,只是没有注释。
    • 你有数据库优先的态度,我个人根本不需要数据库,因为如果一切都是基于接口的,数据库只是一个实现细节,但这不是讨论的地方这。如果我有帮助,请考虑支持答案并将其标记为解决方案。祝你好运。如果您对此感兴趣,请阅读有关 SOLID & GRASP 原则、DDD 模式、干净代码等的更多信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2018-06-03
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多