[英]Doctrine One to many - Symfony3
how I can relation 2 tables in doctrine? 我怎样才能将2个表格联系在一起?
I have a entity Storyboard
and this entity has a entities Story
. 我有一个实体
Storyboard
,这个实体有一个实体Story
。
My entity Storyboard
: 我的实体
Storyboard
:
/**
* @var int
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="smallint", options={"unsigned": true})
* @Serializer\Expose()
*/
private $id;
/**
* @var Story
* @OneToMany(targetEntity="AppBundle\Entity\Story", mappedBy="storyboard")
* @ORM\joinColumn={@ORM\JoinColumn(name="story", referencedColumnName="id")}
* @Serializer\Expose()
*/
private $story;
/**
* @return Story
*/
public function getStory(): Story
{
return $this->story;
}
/**
* @param Story $story
*/
public function addStory(Story $story)
{
if (!$this->story->contains($story)){
$this->story->add($story);
}
}
/**
* @param Story $story
*/
public function removeStory (Story $story){
if($this->story->contains($story)){
$this->story->removeElement($story);
}
}
and my Entity Story
: 和我的实体
Story
:
/**
* @var int
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="smallint", options={"unsigned": true})
* @Serializer\Expose()
*/
private $id;
This is my StoryboardController
这是我的
StoryboardController
$storyboard->addStory($story);
When I run this I have this error: 当我运行此程序时,出现以下错误:
Undefined index: storyboard
Edit
编辑
I try this for the Story
constructor: 我尝试使用
Story
构造函数:
/**
* Story constructor.
* @param $userId
* @param string $name
* @param string $description
* @param string $categoryId
* @param Storyboard $storyBoard
* @internal param int $currentStage
* @internal param \DateTime $createAt
* @internal param \DateTime $updateAt
*/
public function __construct($userId, $name, $description, $categoryId, $storyBoard)
{
$this->userId = $userId;
$this->name = $name;
$this->description = $description;
$this->currentStage = 0;
$this->categoryId = $categoryId;
$this->createAt = new \DateTime();
$this->updateAt = new \DateTime();
$this->storyboard = $storyBoard;
}
and this is the StoryRepository
to create a new Story
这是创建新
Story
的StoryRepository
/**
* @param string $userID
* @param string $name
* @param string $description
* @param $categoryId
* @param Storyboard $storyBoardId
* @return Story story
*/
public function createNewStory($userID, $name, $description, $categoryId, $storyBoard)
{
$story = new Story($userID, $name, $description, $categoryId, $storyBoard);
$this->_em->persist($story);
$this->_em->flush();
return $story;
}
but I recive an error saying that the parameter $storyBoard
is an array, not a Storyboard
entity in line: $this->_em->flush()
但是我收到一个错误,说参数
$storyBoard
是一个数组,而不是一行中的一个Storyboard
实体: $this->_em->flush()
$storyBoard
$this->_em->flush()
This is how you should declare your entities : 这是您应该如何声明实体的方法:
Storyboard
故事板
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Story", mappedBy="storyboard", cascade={"ALL"}, orphanRemoval=true)
*/
private $stories;
....
public function __construct()
{
$this->stories = new ArrayCollection();
}
...
/**
* @param Story $story
*/
public function removeStory(Story $story)
{
$this->stories->removeElement($story);
}
/**
* @return ArrayCollection
*/
public function getStories()
{
return $this->stories;
}
/**
* @param ArrayCollection $stories
*/
public function setStories($stories)
{
foreach ($stories as $story) {
/** @var Story $story */
if (is_null($story->getStoryboard())) {
$story->setStoryboard($this);
}
}
$this->stories = $stories;
}
Story
故事
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Storyboard
*
* @ORM\ManyToOne(targetEntity="Storyboard", inversedBy="stories")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $storyboard;
...
// normal getter and setter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.