14

I've one type array field in Entity,

MyEntity.php

/**
 * @var string
 *
 * @ORM\Column(name="excepcionMenu", type="array", length=255, nullable=true)
 */
private $excepcion;

I would like to get a QueryBuilder to select not empty or not null in $excepcion field.

I'm trying MyEntityRepository.php

public function findAllExcepcionesByItem($itemId) {

    $query = $this->createQueryBuilder('p')
            ->leftJoin('p.item', 'i')
            ->where('i.id = :actual')->setParameter('actual', $itemId)
            ->andWhere('p.excepcion IS NOT NULL')
            ->getQuery();

    return $query->getResult();
}

But this returns all table records.

public function findAllExcepcionesByItem($itemId) {

    $query = $this->createQueryBuilder('p')
            ->leftJoin('p.item', 'i')
            ->where('i.id = :actual')->setParameter('actual', $itemId)
            ->andWhere('p.excepcion IS NULL')
            ->getQuery();

    return $query->getResult();
}

But this returns zero records.

this field in the database stores the values in this way:

a:0:{} // empty
N; // null
a:2:{i:0;i:2;i:1;i:4;} // not empty or not null

Is it possible to do this with QueryBuilder or should be done with DQL?

thanks a lot


UPDATED solution contributed by @Attila Szalay

public function findAllExcepcionesByItem($itemId) {

    $query = $this->createQueryBuilder('p')
            ->leftJoin('p.item', 'i')
            ->where('i.id = :actual')->setParameter('actual', $itemId)
            ->andWhere('p.excepcion != :null')->setParameter('null', serialize(null)) //not null
            ->andWhere('p.excepcion != :empty')->setParameter('empty', serialize([])) //not empty
            ->getQuery();

    return $query->getResult();
}
4
  • Why $excepcion is string typing and column type is array? Commented Jan 12, 2016 at 12:37
  • 1
    because it has to save several possible options. Form on Twig renders several checkbox. Commented Jan 12, 2016 at 12:41
  • Do you know that an array cannot be null in PHP? Commented Jan 12, 2016 at 12:46
  • I know, but the array type in doctrine is not exactly an array in php. Removing nullable=true of the field definition in the entity, the result is the same, to create a new empty item, the value stored in the database is still N; and the query result too. Commented Jan 12, 2016 at 12:56

4 Answers 4

16

The other solution for the problem which worked for me is:

public function findAllExcepcionesByItem($itemId) {
    $query = $this->createQueryBuilder('p')
        ->leftJoin('p.item', 'i')
        ->where("i.id = :actual")->setParameter("actual", $itemId)
        ->andWhere("p.excepcion != ''") // NOT EMPTY
        ->andWhere("p.excepcion IS NOT NULL") // NOT NULL
        ->getQuery();
    return $query->getResult();
}
Sign up to request clarification or add additional context in comments.

1 Comment

->andWhere("p.excepcion IN NOT NULL") // NOT NULL IN or IS ? :)
5

Your data stored as serialized "string" in your database so NULL value will be a "N;" string and it is not a NULL value for db engines.

Try this:

 $query = $this->createQueryBuilder('p')
        ->leftJoin('p.item', 'i')
        ->where('i.id = :actual')->setParameter('actual', $itemId)
        ->andWhere('p.excepcion != :null')->setParameter('null', 'N;') //not null
        ->getQuery();

2 Comments

You're right, and had not fallen into this detail :) Now I only need check for a:0:{} thanks
You can use serialize(null) for NULL check instead of 'N;' string and serialize([]) for empty array.
2
$qb = $this->createQueryBuilder('p');
$query = $qb->leftJoin('p.item', 'i')
        ->where('i.id = :actual')->setParameter('actual', $itemId)
        ->andWhere($qb->expr()->isNotNull("p.excepcion"))
        ->getQuery();

In short you need to use the Expr class, which is explained in further detail in the QueryBuilder chapter of Doctrine's documentation. I just showed you how to use it, however!

1 Comment

thank you very much, but the result is exactly the same as without using regular expressions. Like ->andWhere('p.excepcion IS NOT NULL'). I think the problem is the type of field array.
0

Your repository method is totally correct.

But, an array cannot be null. If it's null, then it's not an array: it's null.

The solution could be to change the type of the column excepcionMenu.

1 Comment

thank you @scoolnico, I have already answered this question in your comment on the main question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.