【问题标题】:How to pass Array of Entities to Entity Repository and run `where in` query?如何将实体数组传递给实体存储库并运行“在哪里”查询?
【发布时间】:2018-07-27 12:33:30
【问题描述】:

我有Plan $plans 的数组。 如何将该数组 $plans 传递给我的成员控制器? 目前我使用: 注释中的* @param Plans|array $plan

    public function findMembersByPlans( $planArray)
    {
        $queryBuilder = $this->createQueryBuilder('m');
        $count = $queryBuilder->select('count(m)')
            ->leftJoin('mp.plan', 'p')
            ->where($queryBuilder->expr()->in('p.plan', ':planArray'))
            ->setParameters(
                [
                    'planArray'=> $planArray,
]);

如何获取成员数?当我传递 1 个对象时。 Plan $plan 作为参数一切正常。

【问题讨论】:

  • 仅从您的 planArray 中提取 id,而不是使用完整的对象。

标签: symfony doctrine dql php-5.6


【解决方案1】:

你可以试试这样的:

->leftJoin('mp.plan', 'p', Join::WITH, $queryBuilder->expr()->in('p.id', ':planIds'))
->setParameteter('planIds', array_map(
    function (Plan $plan) {
        return $plan->getId();
    }, $planArray
))

你可以删除 where 子句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 2012-03-25
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多