drvonr6573 2017-10-13 10:05
浏览 102
已采纳

symfony @oneToMany关系仅返回集合中的最后一个对象

I have a situation and not really sure what I'm doing wrong. in Sumfony 3.3 I've created a relation between entity Page and Language, where Page is related to multiple Languages, and when I search for a Page and get Page object but property Languages returns collection with only last Language object. No matter how many objects are there in collection it always returns last.

Page Entity:

/**
 * @ORM\OneToMany(targetEntity="Language", mappedBy="page", cascade={"ALL"}, indexBy="page_id")
 */
private $languages;

Language entity:

/**
 * @ORM\ManyToOne(targetEntity="Page", inversedBy="languages")
 */
private $page;

public function addLanguage(\AppBundle\Entity\Langaugee $language)
{
    $this->languages[] = $language;

    return $this;
}

public function removeLanguage(\AppBundle\Entity\Language $language)
{
    $this->$languages->removeElement($language);
}

public function getLanguages()
{
    return $this->languages;
}

Page object is fetching in PageService:

public function getPageByName($name)
{
return $this->pageRepository->findBy(array("name"=>$name));
}

Since property $languages by default set on lazy, JMS serializer when serializes Page object it's fetching languages collection

Did anyone had this problem?

  • 写回答

1条回答 默认 最新

  • du9826 2017-10-13 20:08
    关注

    After thorough debugging, I figure it out that indexBy is misused here. Defined indexBy = page_id provided always the same value so every record that is mapped to entity in SimpleObjectHydrator overrun the existing record, leaving only last added Language object in collection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?
  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?