对象包含太多列

Object includes too many columns

我有一个 MySQL table,其中包含以下列:

我使用以下代码接收数据:

$user = new Users();
$res = $this->getDoctrine()->getRepository(Users::class)->findOneBy([
    'email'    => $data['email'],
    'password' => $data['password']
]);

输出为:

object(App\Entity\Users)#427 (5) { ["id":"App\Entity\Users":private]=> int(14) ["email":"App\Entity\Users":private]=> string(14) "test@email.com" ["password":"App\Entity\Users":private]=> string(7) "mypw123" ["registerDate":"App\Entity\Users":private]=> int(2019) ["active":"App\Entity\Users":private]=> bool(false) }

好吧,那个对象 return 的数据比我需要的多。我需要的只是 emailpassword.

列中的值

我如何 return 一个对象或数组,return 只是我需要的列中的值?

试试这个:

$res = $this->getDoctrine()->getRepository(Users::class)->findBy([
    'email' => $data['email'],
    'password' => $data['password']
]);

if (count($res) > 0) {
    //found
}

或类似建议@u_mulder:

$entityManager = $this->getDoctrine()->getManager();
$qb = $entityManager->createQueryBuilder()
        ->select('count(u.id)')
        ->from(Users::class, 'u')
        ->where('u.email = :email')
        ->andWhere('u.password = :password')
        ->setParameter('email', $data['email'])
        ->setParameter('password', $data['password']);

if ($qb->getQuery()->getSingleScalarResult() > 0) {
    //found
}