gpt4 book ai didi

doctrine - Symfony 4 如何实现 Doctrine XML ORM 映射

转载 作者:行者123 更新时间:2023-12-01 11:15:07 25 4
gpt4 key购买 nike

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>

祝你好运。
  • Version 2.6 XML mapping reference
  • 关于doctrine - Symfony 4 如何实现 Doctrine XML ORM 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52962708/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com