簡體   English   中英

一對一的學說-Symfony3

[英]Doctrine One to many - Symfony3

我怎樣才能將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;
}

這是創建新StoryStoryRepository

    /**
 * @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() $storyBoard $this->_em->flush()

這是您應該如何聲明實體的方法:

故事板

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM