我有两个实体Game和GameSource。游戏只有一个GameSource。
Game.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Table(name="game")
* @ORM\Entity(repositoryClass="AppBundle\Repository\GameRepository")
*/
class Game
{
/**
* @ORM\Column(type="tinyint",options={"unsigned" = true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\OneToOne(targetEntity="GameSource",mappedBy="game",cascade={"persist"})
* @Assert\Type(type="AppBundle\Entity\GameSource")
* @Assert\Valid()
*/
private $gameSource;
}
GameSource.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Table(name="game_source")
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\GameSourceRepository")
*/
class GameSource
{
/**
* @ORM\Column(type="smallint",options={"unsigned" = true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="Game",inversedBy="gameSource")
* @ORM\JoinColumn(nullable=false)
*/
private $game;
}
表格GameType.php
<?php
namespace AppBundle\Form;
use AppBundle\Entity\Game;
use AppBundle\Form\GameSourceType;
use Symfony\Component\Form\{
AbstractType,
FormBuilderInterface
};
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\{
TextType,
CheckboxType
};
class GameType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, [
'label' => 'Nazwa rozgrywki',
'required' => true
])
->add('gameSource', GameSourceType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Game::class,
));
}
}
GameController.php - addAction
public function addAction(Request $request)
{
$gameEntity = new Game();
$form = $this->createForm(GameType::class, $gameEntity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($gameEntity);
$em->flush();
}
}
当我尝试坚持记录时我有错误
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'game_id' cannot be null
问题在于: 当我尝试保存他时,实体GameSource没有看到游戏的ID。谁知道我做错了什么?
答案 0 :(得分:1)
使用您的实体,当您调用setGameSource()
函数时,您需要将Game实体与GameSource联系起来。例如,而不是:
public function setGameSource(?GameSource $gameSource): self
{
$this->gameSource = $gameSource;
return $this;
}
你会想要这样做:
public function setGameSource(?GameSource $gameSource): self
{
$this->gameSource = $gameSource;
$this->gameSource->setGame($this);
return $this;
}
这样实体就可以正确连接。