jolicode / elastically

🔍 JoliCode's Elastica wrapper to bootstrap Elasticsearch PHP integrations
248 stars 37 forks source link

array of nested objects not transformed to objects #88

Closed bogdandubyk closed 3 years ago

bogdandubyk commented 3 years ago

so if I have a dto like

final class SomeDto {

   /**
     * @param int $id
     * @param string $name
     * @param SeconfDto $secondDto
     * @param array<ThirdDto> $otherDtos
     */
    public function __construct(publid int $id, public string $name, public SeconfDto $secondDto, public array $otherDtos) {
    }
}

and retrieving it from the elastic search using $index->getModel(1); it's not converting $otherDtos to instances of ThirdDto, but simply return as an array of associative arrays.

Here is the mapping file:

mappings:
    dynamic: false
    properties:
        name:
            type: text
            analyzer: english
            fields:
                autocomplete:
                    type: text
                    analyzer: app_autocomplete
                    search_analyzer: standard
        secondDto:
            type: object
            properties:
                name:
                    type: text
                    analyzer: english
                    fields:
                        autocomplete:
                            type: text
                            analyzer: app_autocomplete
                            search_analyzer: standard
        otherDtos:
            type: object
            properties:
                name:
                    type: text
                    analyzer: english
                    fields:
                        autocomplete:
                            type: text
                            analyzer: app_autocomplete
                            search_analyzer: standard

Is this not supported to use nested dto's, or maybe I'm doing something wrong?????

bogdandubyk commented 3 years ago

so I'm inserting dto like (it's a dump of dto):

SomeDto {#678
  +id: 1
  +name: "Liza Kessler"
  +secondDto: SeconfDto {#709
    +id: 1
    +name: "Conroy-Wisoky"
  }
  +otherDtos: array:1 [
    0 => ThirdDto {#705
      +id: 1
      +name: "Wiegand-Jerde"
    }
    1 => ThirdDto {#705
      +id: 2
      +name: "Conroy-Wisoky"
    }
  ]
}

but getting back it in a format like :

SomeDto {#678
  +id: 1
  +name: "Liza Kessler"
  +secondDto: SeconfDto {#709
    +id: 1
    +name: "Conroy-Wisoky"
  }
  +otherDtos: array:1 [
    0 => array:2 [
      +id: 1
      +name: "Wiegand-Jerde"
    ]
    1 =>  array:2
      +id: 2
      +name: "Conroy-Wisoky"
    ]
  ]
}
bogdandubyk commented 3 years ago

looks like it's a Symfony serializer issue.. BTW I'll leave this open for a some time, maybe I'll get some help,

bogdandubyk commented 3 years ago

I was right, it serializer issue, I fix it by adding

public function addOtherDtos(ThirdDto $dto) {
    $this->otherDtos = $dto
}

not the best experience if dto should be immutable, but it also work with a `private modifier so it still can be immutable