从多行中获取数据

Get data from multiple rows

我想从 所有 行中获取列 uid 的值,其中 city 列的值为 london .

我试过这样的:

$this->getDoctrine()->getRepository(Personal::class)->findOneBy(['city' => 'london']);

但后来我只从 1 行中得到一个对象。我需要的只是一个看起来像这样的数组:

array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }

尝试使用 findBy

$this->getDoctrine()->getRepository(Personal::class)->findBy(['city' => 'london']);

findOneBy 方法return只有一行

Simple Conditions

使用 findBy 代替 findOneBy。它为您提供实体数组。

但您无法定义应该获取哪一列。 为此,您应该在定义应获取的内容的地方使用 DQL。

例如使用查询构建器:

$queryBuilder = $this->createQueryBuilder()
    ->select('p.uid')
    ->from(Personal::class, 'p')
    ->where('p.city = \'london\'');

$uids = $queryBuilder->getQuery()->getResult();