How to use doctrine criteria to filter array properties?

I am adding virtual property to the Symfony entity class. This property should be calculated based on other tabular data - in particular, for a column of the Doctrine array type .

class RelatedEntity
{

    /* ... */

    /**
     * @ORM\Column(type="array")
     */
    protected $type;

Point I would like to use the Doctrine criterion for this, since it must be optimized at the SQL level. So I did this:

public function getCreated()
{
    $criteria = Criteria::create()->where(Criteria::expr()->contains('type', 'create'));
    $relatedEntity = $this->getRelatedEntities()->matching($criteria);

    if (!$relatedEntity) {
        return null;
    }

    return $relatedEntity->getTimestamp();
}

But I get an empty result set. Despite the fact that Doctrine creates the correct SQL statement that works when I manually enter it into the PostgreSQL database.

...WHERE type LIKE '%create%'

? ArrayCollection, , .

.

EDIT. , EntityManager EntityRepository . , , , .

+6
1

getRelatedEntities()

, , . , , Criteria.

  • , (: QueryBuilder join/select).
    • getRelatedEntities Doctrine QueryBuilder, , , .
    • EX.: $queryBuilder->addSelect('thing')->leftJoin('root_alias.entity', 'thing')
    • :
      • Criteria::expr()->contains('thing.type', 'create')
  • .
    • , , .

, , , . , , .

$criteria = Criteria::create()->where(Criteria::expr()->contains('type', 'create'));

$collection = new ArrayCollection([
    [
        'key' => 1,
        'type' => 'somethingcreatesomething',
    ],
    [
        'key' => 2,
        'type' => 'abra',
    ],
    [
        'key' => 3,
        'type' => 'cadabra',
    ],
    [
        'key' => 4,
        'type' => 'alacreate',
    ],
]);

dump($collection->matching($criteria));

Doctrine\Common\Collections\ArrayCollection {#2536
  -elements: array:2 [
    0 => array:2 [
      "key" => 1
      "type" => "somethingcreatesomething"
    ]
    3 => array:2 [
      "key" => 4
      "type" => "alacreate"
    ]
  ]
}
0

Source: https://habr.com/ru/post/1014065/


All Articles