2

Doctrine can automatically serialize simple PHP array values (see https://www.doctrine-project.org/projects/doctrine-dbal/en/2.10/reference/types.html#array-types).

Example

<?php
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="my_table")
 */
class MyTable {
    /**
     * @var array
     *
     * @ORM\Column(name="my_array", type="json", nullable=true)
     */
    private $myArray;
}

Example in database

|   my_array    |
|---------------|
|'["FOO","BAR"]'|

Question

Is there a way to query these values without some sort of ugly regex directly by DQL or with query builder?

$queryBuilder
    ->andWhere(":searchParam IN myTable.myArray")
    ->setParameter('searchParam', 'FOO');

Side note: I could always wrap the searchParam and search with LIKE key word. However I am looking for an alternative solution.

3
  • What database system are we talking about? For example, PostgreSQL has JSONB type, which can be nicely queried. I'm not familiar with MySQL but there's also JSON data type. Commented Jul 31, 2018 at 14:16
  • PostgresSql witch does has its own JSON type. But I am limited with what can Doctrine and its DQL do. Commented Jul 31, 2018 at 14:45
  • Right, that's why I don't use Doctrine :) Nevertheless, I believe that you can use raw SQL with Doctrine, but I'm not an expert on this. Commented Jul 31, 2018 at 14:58

1 Answer 1

1

No, there is no such way. references: first second

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately true :-\ I am marking this as correct answer until Doctrine implements this.

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.