【发布时间】: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