教义一对多 - Symfony3

时间:2017-03-03 08:58:21

标签: php doctrine symfony

我如何在学说中关联2个表?

我有一个实体Storyboard,这个实体有一个实体Story

我的实体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);
    }
}

和我的实体Story

/**
* @var int
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="smallint", options={"unsigned": true})
* @Serializer\Expose()
*/
    private $id;

这是我的StoryboardController

 $storyboard->addStory($story);

当我运行时,我有这个错误:

Undefined index: storyboard
  

修改

我为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;
}

这是创建新StoryRepository

Story
    /**
 * @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;
}

但我收到一条错误消息,说参数$storyBoard是一个数组,而不是Storyboard实体$this->_em->flush()

1 个答案:

答案 0 :(得分:0)

这是你应该如何宣布你的实体:

  

故事板

/**
 * @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;
}
  

故事

/**
 * @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