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;
}
}
Run Code Online (Sandbox Code Playgroud)
首先定义自己的映射:
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
Run Code Online (Sandbox Code Playgroud)
文件名必须匹配的模式[class_name].orm.xml,你的情况Customer.orm.xml。如果内部有子命名空间,例如 值对象YourDomain\Entity\ValueObject\Email,该文件必须命名ValueObject.Email.orm.xml。
映射示例:
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
Run Code Online (Sandbox Code Playgroud)
祝好运。