Home > database >  Or on symfony findOneBy
Or on symfony findOneBy

Time:10-20

Is possible to make a request by one filed or another using the orm on symfony

$user_test = $em->getRepository("AppBundle:UserTest")->findOneBy([
                    'hash' => $data->user_data->hash,
                    'code' => $data->user_data->hash],
                );

to get something like WHERE hash = 'foo' OR code = 'foo'

CodePudding user response:

you have to define the method in the entity UserTest repository

// UserTestRepository.php

public function getUserTestByHashOrCode($hash, $code){
    return $this->createQueryBuilder('u')
        ->where('hash = :hash')
        ->orWhere('code = :code')
        ->setParameter('hash', $hash)
        ->setParameter('code', $code)
        ->getQuery()
        ->getOneOrNullResult();

and then

$user_test = $em->getRepository("AppBundle:UserTest")->getUserTestByHashOrCode($data->user_data->hash, $data->user_data->hash);
  • Related