如何使用负条件使用查询生成器筛选相关实体



我的实体:

  /**
  * @ORMManyToMany(targetEntity="Productgroup", inversedBy="fields")
  * @ORMJoinColumn(name="productgroup", referencedColumnName="id")
  */
  private $productgroup;
  public function getProductgroup()
  {
    return $this->productgroup;
  }
  public function setProductgroup($productgroup): self
  {
    $this->productgroup = $productgroup;
    return $this;
  }
  public function __construct()
  {
    $this->productgroup = new ArrayCollection();
  }

这是输出:

array:2 [▼
  0 => Fields {#7534 ▼
    -id: 3
    -name: "cat"
    -unique_id: "5a38c820ed"
    -productgroup: PersistentCollection {#7538 ▼
      -snapshot: array:1 [ …1]
      -owner: Fields {#7534}
      -association: array:20 [ …20]
      -em: EntityManager {#2889 …11}
      -backRefFieldName: "fields"
      -typeClass: ClassMetadata {#6568 …}
      -isDirty: false
      #collection: ArrayCollection {#7539 ▼
        -elements: array:1 [▼
          0 => Productgroup {#7220 ▼
            -id: 6
            -name: "Animals"
            -unique_id: "9e4ef1c46f"
            -fields: PersistentCollection {#7431 ▶}
          }
        ]
      }
      #initialized: true
    }
    -type: Type {#7615 ▶}
  }
  1 => Fields {#7616 ▼
    -id: 5
    -name: "horse"
    -unique_id: "c3890b9287"
    -productgroup: PersistentCollection {#7617 ▼
      -snapshot: []
      -owner: Fields {#7616}
      -association: array:20 [ …20]
      -em: EntityManager {#2889 …11}
      -backRefFieldName: "fields"
      -typeClass: ClassMetadata {#6568 …}
      -isDirty: false
      #collection: ArrayCollection {#7618 ▼
        -elements: []
      }
      #initialized: false
    }
    -type: Type {#7619 ▶}
  }
]

我正在尝试做的是删除所有与生产组 id 6 没有关系的数组。

到目前为止,我可以弄清楚如何删除所有与产品组 ID 6没有关系的数组:

控制器:

$group = $this->getDoctrine()->getRepository($EntityName)->filterByColletion(6);

和存储库:

  public function filterByColletion($id)
    {
      return $this->createQueryBuilder('f')
      ->leftJoin('f.productgroup', 'pg')
      ->where('pg.id = :id')
      ->setParameter(':id', 6)
      ->getQuery()
      ->execute();
    }

结果是:

array:1 [▼
  0 => Fields {#7534 ▼
    -id: 3
    -name: "cat"
    -unique_id: "5a38c820ed"
    -productgroup: PersistentCollection {#7538 ▼
      -snapshot: array:1 [ …1]
      -owner: Fields {#7534}
      -association: array:20 [ …20]
      -em: EntityManager {#2889 …11}
      -backRefFieldName: "fields"
      -typeClass: ClassMetadata {#6568 …}
      -isDirty: false
      #collection: ArrayCollection {#7539 ▼
        -elements: array:1 [▼
          0 => Productgroup {#7220 ▼
            -id: 6
            -name: "Animals"
            -unique_id: "9e4ef1c46f"
            -fields: PersistentCollection {#7431 ▶}
          }
        ]
      }
      #initialized: true
    }
    -type: Type {#7615 ▼
      +__isInitialized__: true
      -id: 3
      -name: "password"
      -unique_id: "2ef6e55a1d"
      -label: "password"
       …2
    }
  }
]

但我需要它的方式恰恰相反。所以我尝试了:

   public function filterByColletion($id)
    {
      return $this->createQueryBuilder('f')
      ->leftJoin('f.productgroup', 'pg')
      ->where('pg.id != :id')
      ->setParameter(':id', 6)
      ->getQuery()
      ->execute();
    }

但这给了我一个空数组作为输出,而不是预期的名为 horse 的字段。

我也尝试了另一种方法,但这并没有奏效:

public function filterByColletion($id)
{
  $em = $this->getEntityManager();
  $qb  = $this->_em->createQueryBuilder();
  return $this->createQueryBuilder('f')
  ->leftJoin('f.productgroup', 'pg')
  ->where($qb->expr()->notIn('pg.id',6))
  ->getQuery()
  ->execute();
}
我相信

您不正确地使用 expr 尝试使用这种方式:

$qb->expr()->notIn('f.productgroup', [6])

你可以在这里阅读更多 如何在原则 2 中使用 WHERE IN

相关内容

  • 没有找到相关文章

最新更新