我正在嘗試創建關系,其中外鍵參考不是主鍵而是復合唯一約束。為什么?非規范化資料庫架構以減少連接計數。
#[ORM\Entity(repositoryClass: CurrencyRepository::class)]
#[ORM\UniqueConstraint(fields: ['slug', 'type'])]
#[UniqueEntity(
fields: ['type', 'slug'],
message: 'This slug is already in use on that type.',
errorPath: 'slug',
)]
class Currency
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id;
#[ORM\Column(type: 'smallint', length: 1)]
private ?int $type;
#[ORM\Column(type: 'string', length: 25)]
private ?string $slug;
// ...
}
#[ORM\Entity(repositoryClass: ExchangeRateHistoryTypeRepository::class)]
class ExchangeRateHistoryType
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private int $id;
#[ORM\ManyToOne(targetEntity: Currency::class)]
#[ORM\JoinColumn(name: 'currency_slug', referencedColumnName: 'slug', nullable: false)]
#[ORM\JoinColumn(name: 'currency_type', referencedColumnName: 'type', nullable: false)]
private ?Currency $currency;
php bin/console make:migration
php bin/console doctrine:migrations:migrate
都好。但是當我嘗試將資料添加到 ExchangeRateHistoryType 時 - 錯誤。客戶端代碼:
$exchangeRateHistoryType = new ExchangeRateHistoryType();
$exchangeRateHistoryType->setCurrency($currency);
// ...
$this->entityManager->persist($exchangeRateHistoryType);
$this->entityManager->flush();
在 BasicEntityPersister.php 第 674 行:警告:未定義的陣列鍵“slug”
我在做什么錯?
uj5u.com熱心網友回復:
Doctrine 的檔案:
不能使用指向非主鍵的連接列。Doctrine 會認為這些是主鍵,并使用資料創建延遲加載代理,這可能會導致意外結果。Doctrine 可以出于性能原因不在運行時驗證此設定的正確性,而只能通過 Validate Schema 命令。
資料來源:https : //www.doctrine-project.org/projects/doctrine-orm/en/2.10/reference/limitations-and-known-issues.html#join-columns-with-non-primary-keys
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/379246.html
