我有點不知所措。
我想將資料匯入我的資料庫。這是一個示例物體:
![執行查詢時發生例外:SQLSTATE [23000]:完整性約束違規:1048 列“pokemon_id”不能為空](https://img.uj5u.com/2022/04/22/a8ac2831665a485aa49a77838ce2320e.png)
所有領域 nesassary 領域都被填滿了,但教義告訴我:
In ExceptionConverter.php line 111:
An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'pokemon_id' cannot be null
In Exception.php line 26:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'pokemon_id' cannot be null
In Statement.php line 92:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'pokemon_id' cannot be null
這是我的物體的片段。
/**
* @ORM\Column(type="integer")
*/
private ?int $pokemonId;
public function getPokemonId(): ?int
{
return $this->pokemonId;
}
public function setPokemonId(int $pokemonId): self
{
$this->pokemonId = $pokemonId;
return $this;
}
我在哪里錯過了重點?
PS:
也許我還應該告訴這個屬性永遠不能為空。我和一個有關系
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Pokemon\Pokemon", inversedBy="pokemon_id")
*/
private $pokemon;
public function getPokemon(): ?Pokemon
{
return $this->pokemon;
}
public function setPokemon($pokemon): self
{
$this->pokemon = $pokemon;
return $this;
}
為什么 SQL 不占用我的財產?
uj5u.com熱心網友回復:
這是解決方案。
當你有一個一對多的關系(在我的例子中是口袋妖怪)你不能setpokemonId()用來存盤關系時,你可以使用setPokemon()。Doctrine 會將值存盤在 pokemon_id 列中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/460456.html
