symfony - 为什么 Doctrine 不返回数组中的值?

标签 symfony doctrine-orm doctrine symfony-3.3

我有一个带有 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);

我有:

enter image description here

但是当我调用它时:

    $qb = $this->createQueryBuilder('nv')
        ->select('nv')
        ->innerJoin('nv.categories', 'nvc')
        ->addSelect('nvc.id');

我有:

enter image description here

为什么 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/

相关文章:

php - Sylius:添加具有可翻译内容的资源

php - 如何从symfony3中的同一表单访问2个表?

php - 如何在 Doctrine 中编码 JSON 数组?

symfony - 使用 Sonata Admin Bundle 更改 xls 导出中的日期格式

JavaScript 在 Symfony 中加载两次

php - 请参阅带有参数的学说查询构建器 DQL

php - 领域驱动设计 - 聚合 - EventPublisher with Doctrine

php - 重建 VM 后 apache2 中的 Symfony2 URL 404

php - symfony 1.4.15 错误与 php 7

php - 无法使用 symfony 和 doctrine 及其关联对象或表保存到 postgresql 中?