簡體   English   中英

Symfony 4 如何實現 Doctrine XML ORM 映射

[英]Symfony 4 how to implement Doctrine XML ORM mapping

Symfony 4 文檔不清楚如何使用 XML orm 映射而不是注釋。 在官方文檔中沒有看到如此重要部分的細節令人沮喪。

想象一下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>

祝你好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM