我有一个带有 ManyToMany 的实体 NewsVersion
:
class NewsVersion
{
...
/**
* @var NewsCategory[]|ArrayCollection
*
* @ORM\ManyToMany(
* targetEntity="AppBundle\Entity\NewsCategory",
* cascade={"persist"}
* )
* @ORM\JoinTable(name="news_version_categories")
*/
private $categories;
...
在我的存储库中,当我调用它时:
$qb = $this->createQueryBuilder('nv')
->select('nv')
->innerJoin('nv.categories', 'nvc')
->addSelect('nvc');
return $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
我有:
但是当我调用它时:
$qb = $this->createQueryBuilder('nv')
->select('nv')
->innerJoin('nv.categories', 'nvc')
->addSelect('nvc.id');
我有:
为什么 nvc.id
不返回 categories
数组中的 id
?我只想从我的类别中返回 id,但是在 NewsVersion
实体中的 categories
数组中(与第一个屏幕相同)
最佳答案
你应该删除 ->addSelect('nvc.id')
并在select语句中加入category的id
$qb = $this->createQueryBuilder('nv')
->select('nv', 'nvc.id')
->innerJoin('nv.categories', 'nvc');
关于symfony - 为什么 Doctrine 不返回数组中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47250293/