【问题标题】:Symfony / Doctrine Mapping many to many , getting ArrayColletionSymfony / Doctrine Mapping many to many , 得到 ArrayColletion
【发布时间】:2018-02-01 11:50:14
【问题描述】:

我在获取两个实体关系的项目时遇到了一些问题。 有三张桌子。 Terceiros - rel_terceiros_flags - 标志。 Terceiros 有许多标志。 而且Flags可以有很多Terceiros。

现在,我只使用两个实体:Terceiros & Flags。关系表没有映射到 orm 实体中。

关于 Terceiros 实体:

/**
 * Many Users have Many Groups.
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Flags")
 * @ORM\JoinTable(name="rel_terceiros_flags",
 *      joinColumns={@ORM\JoinColumn(name="id_sage", referencedColumnName="id_sage")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="id_flag", referencedColumnName="id_flag")}
 *      )
 */
private $flagsTer;

/**
 * Terceiros constructor.
 */
public function __construct()
{
    $this->flagsTer = new ArrayCollection();
}

/**
 * @return ArrayCollection
 */
public function getFlagsTer()
{
    return $this->flagsTer;
}

当我通过 id 从 Terceiros 搜索中渲染对象时,ArrayColletion 为空。我在关系表上有 3 行,同一个 Terceiro 有 3 个标志。当我做转储时,我得到这样的东西:

#collection: ArrayCollection {#2463 ▼
  -elements: []
}

在控制器上:

/**
 * SubGrid Example
 *
 * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
 * @throws \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
 * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
 * @throws \Symfony\Component\Form\Exception\LogicException
 * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException
 * @throws \LogicException
 * @throws \InvalidArgumentException
 *
 * @Method("GET")
 * @Route("/terceiros/example",options={"expose"=true}, name="app_terceiros_example")
 * @throws \LogicException
 * @param Request $request
 * @return Response
 * @throws \Exception
 */
public function terFlagsAction(Request $request)
{

    $terceirosRepos = $this->getDoctrine()->getRepository('AppBundle:Terceiros');
    $terceiros = $terceirosRepos->find('00001');
    return $this->render(':app/terceiros:terceiros_flags.html.twig',['terceiros' => $terceiros]);
}

在树枝视图上:

{{ dump(terceiros) }}

你能帮帮我吗?

【问题讨论】:

  • 您使用什么查询来检索您的Terceiros 实体?您使用什么来获取消息末尾显示的ArrayCollection 的转储?
  • 抱歉没有具体说明,我像这样在 Twig 上转储 Terceiros 对象 {{ dump(user) }}
  • 问题已编辑,现在检查:)

标签: php symfony doctrine-orm orm many-to-many


【解决方案1】:

试试这个;

dump($terceiros->getFlagsTer()->getValues());

【讨论】:

  • 抱歉不具体,我像这样 {{ dump(user) }} 将 Terceiros 对象转储到 Twig 上
  • 问题已编辑,现在检查:)
  • 谢谢!有效!!!! #collection: ArrayCollection {#3518 ▼ -elements: array:4 [▼ 0 => 标志 {#3528 ▶} 1 => 标志 {#3530 ▶} 2 => 标志 {#3531 ▶} 3 => 标志 {#3532 ▶}
  • 酷!由于延迟加载? @pedro 可以修复一些加载注释吗?
  • 我只是不明白为什么我们需要将 ArrayColletion 与转储相结合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多