[英]Symfony Doctrine - Id length in ManyToOne relationship
當我執行“ doctrine:migrations:diff”時,Symfony不會“導出”字段長度,而是放置“ 255”,而“ doctrine:migrations:migrate”不起作用。
我的兩個實體ORM定義:
App\Resources\Domain\Stage:
type: entity
fields:
id:
type: stage_id
id: true
length: 36
name:
type: string
manyToOne:
educationalAreaId:
targetEntity: EducationalArea
joinColumn:
name: educationalAreaId
referencedColumnName: id
App\Resources\Domain\EducationalArea:
type: entity
fields:
id:
type: educational_area_id
id: true
length: 36
name:
type: string
我試圖在manyToOne定義中設置'length'屬性,但沒有成功。
(我在Ids中使用字符串類型,因為會生成UUID)
IMO學說注釋是映射實體的更方便的方法,但是無論如何,我認為您對字段類型做錯了。 type
屬性定義用於數據庫中列的映射類型,因此它應該是string
, integer
, json_array
等。
根據Doctrine docs,通常將id字段與其他字段分開定義,如下所示:
App\Resources\Domain\Stage:
type: entity
table: products
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
manyToOne:
educationalAreaId:
targetEntity: EducationalArea
joinColumn:
name: educationalAreaId
referencedColumnName: id
正如在文檔中所說的:“它內部嵌套了一個generator標簽,它指定主鍵生成機制應自動使用數據庫平台的本機ID生成策略”。
但是您可以在這里找到其他策略。
因此,您可以嘗試以下操作:
App\Resources\Domain\Stage:
type: entity
table: products
id:
id:
type: string
length: 36
generator:
strategy: UUID
...
就我而言,我使用注釋,並且可以正常工作:
/**
* @var string
* @ORM\Column(name="id", type="string", length=36, options={"fixed":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="UUID")
*/
private $id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.