darsyn / ip

Immutable value object for IPv4 and IPv6 addresses, including helper methods and Doctrine support.
http://darsyn.github.io/ip/
MIT License
245 stars 21 forks source link

Problem with QueryBuilder #87

Open FredDut opened 2 years ago

FredDut commented 2 years ago

Hello, I'm facing a problem with querybuilder (Symfony 5.4): I've created a basic entity:

/**
 * @ORM\Entity(repositoryClass=JustipRepository::class)
 */
class Justip
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="ip", nullable=true)
     */
    private $ip;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getIp()
    {
        return $this->ip;
    }

    public function setIp($ip): self
    {
        $this->ip = $ip;

        return $this;
    }
}

in my controller

$ip = Multi::factory('192.168.0.1');

if use the magic method , it 's ok $entities = $this->em->getRepository(Justip::class)->findByIp($ip->getBinary());

if use the querybuilder it's not ok

  public function findByExampleField($value): array
   {
       return $this->createQueryBuilder('j')
           ->andWhere('j.ip = :val')
           ->setParameter('val', $value)
           ->getQuery()
           ->getResult()
       ;
   }

$entities = $this->em->getRepository(Justip::class)->findByExampleField($ip->getBinary());

With findByIp , the runable query is SELECT j0_.id AS id_0, j0_.ip AS ip_1 FROM justip j0_ WHERE j0_.ip = \0x00000000000000000000FFFFC0A80001;

With querybuilder the runable query is SELECT j0_.id AS id_0, j0_.ip AS ip_1 FROM justip j0_ WHERE j0_.ip = 0x00000000000000000000FFFFC0A80001;

I don't know how to tell doctrine that the binary value is binary. Is there something wrong in my code?

zanbaldwin commented 2 years ago

Hi, thanks for reporting this - I'll take a look at this and get back to you!