Elasticsearch with symfony - Error populate command softdeletable entity - symfony

I use the symfony bundle Foselasticabundle and i am facing a problem.
I have an entity (B) which can be deleted via gedmo softdeleteable.
I created the mapping, via the YAML file, and when I execute the following command fos:elastica:populate i get an error.
Entity of type 'App\Entity\B' for IDs id(XX) was not found
In fact, value was previously deleted in my database...
I would have liked him to insert an empty value in the field
Do you have a solution?
Thank you for your answers
fos_elastica.yaml
clients:
default: { url: '%env(ELASTICSEARCH_URL)%/' }
indexes:
app:
types:
A:
properties:
id: ~
name: ~
B:
type: object
id: ~
persistence:
driver: orm
model: App\Entity\A

Related

How to do FOSElasticaBundle bulk index?

It seems that the default doctrine listener used by FOSElasticaBundle does not support bulk index by default. I have an application where I want to add support for more complex search queries through ElasticSearch. The search engine only will perform queries through one unique entity Post. When I create, edit or delete there is not any problem, the index in elasticsearch is updated automatically through the listener. My problem comes when I want to do bulk updates to hide or show more than one post at once, the listener is not receiving the signal to make the bulk index update in elasticsearch.
I am new to FOSElasticSearch so I do not know if I am missing something. I am using FOSElasticaBundle 6, Symfony 5.2 and ElasticSearch 7
Here you can find my fos_elastica.yaml
fos_elastica:
messenger: ~
clients:
default: { host: 127.0.0.1, port: 9200 }
indexes:
product:
settings:
index:
analysis:
analyzer:
my_analyzer:
type: snowball
language: English
persistence:
# the driver can be orm, mongodb or phpcr
driver: orm
model: App\Entity\Post
listener: { enabled: true }
provider: ~
finder: ~
elastica_to_model_transformer:
ignore_missing: true
properties:
title: { boost: 10, analyzer: my_analyzer }
tags: { boost: 8, analyzer: my_analyzer }
description: { boost: 6, analyzer: my_analyzer }
ispublished: { type: integer}
And here you can find the way I am updating more than once entity element at once in PostRepository (the function is to update all post from one unique author, it is just an example):
public function bulkUpdate($ispublished, $authorid){
return $this->createQueryBuilder('p')
->update()
->set('p.ispublished', $ispublished)
->andWhere('p.authorid = :id')
->setParameter('id', $authorid)
->getQuery()
->execute();
}
Also I found that I could disable default listener, dispatch messages for each create, update or delete action through symfony/messenger and consume them async in the background. I guess that I should create my own handler and dispatch specific messages (although I could not find an example about this in the doc) in each modifying action, although at the end I also have the same problem, as I do not know how to send a bulk index update/delete action
In the other hand I was thinking in executing all time a background script in python to check what rows were modified in mysql database and update those index with the script directly through ElasticSearch Api
I do not think that I will need to update more than 1k posts at once, so I would like to keep using the default listener to update posts automatically and avoid gaps between that an entity is modified and the index is updated in ElasticSearch. I just need to find the best way to update indexes in bulk as I have everything else already implemented and working
Sorry for all the text but I wanted to give all details about what I need to do

Doctrine manyToMany Definition not Working Undefined index: joinTable

I am trying to get a manyToMay bi-directional mapping working on Symfony 3.2.6 / PHP 7.1. I am unable to get the
php bin/console doctrine:schema:update --dump-sql
command to run without an error
[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Undefined index: joinTable
Definition is as follows:
Busybee\StudentBundle\Entity\Student:
type: entity
table: student
repositoryClass: Busybee\StudentBundle\Repository\StudentRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
startAtSchool:
type: date
manyToMany:
enrolments:
targetEntity: Busybee\StudentBundle\Entity\Enrolment
mappedBy: students
and
Busybee\StudentBundle\Entity\Enrolment:
type: entity
table: enrolment
repositoryClass: Busybee\StudentBundle\Repository\EnrolmentRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
status:
type: string
length: '32'
lastModified:
type: datetime
createdOn:
type: datetime
manyToMany:
students:
targetEntity: Busybee\StudentBundle\Entity\Student
inversedBy: enrolments
If I remove the mappedBy in the Student Entity the SQL will generate using the doctrine:schema:update command. The example at http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#owning-and-inverse-side-on-a-manytomany-association shows the joinTable index on the inversedBy entity, and adding the joinTable to this or the mappedBy entity still generates the error Undefined index: joinTable
So, what if anything am I doing wrong? Is this a bug? Any help much appreciated.
Craig
I found the issue here. Not a Doctrine problem, but a subscriber I had written to add a table prefix. I have updated the code for the Table Prefix subscriber to correctly capture when the definition is a manyToMany association and to ignore the non-owning side. I am now using the Table Prefix code from the Doctrine Site at http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/sql-table-prefixes.html
Craig.

FOSElasticaBundle NumberFormatException error on populate

I use FOSElasticaBundle in my Symfony 2 project. Since today reindexing is resulting in the below error:
index: /app/hotel/1 caused MapperParsingException[failed to parse
[priceFrom]]; nested: NumberFormatException[For input string:
"410.00"];
In my doctrine orm yml the priceFrom field is defined as followed:
priceFrom:
type: decimal
nullable: true
precision: 7
scale: 2
comment: ''
column: price_from
My fos_elastica config looks like this (config.yml):
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
app:
types:
hotel:
mappings:
id: ~
active: ~
priceFrom: { type: integer }
persistence:
driver: orm
model: XXX\XXXBundle\Entity\Hotel
provider: ~
listener:
immediate: ~
finder: ~
The command I use to reindex: php app/console fos:elastica:populate
The above setup has worked until now. I hope someone can point my to the good direction to solve this problem.
Versions:
ruflin/elastica (2.1.0)
friendsofsymfony/elastica-bundle (v3.1.5)
symfony/symfony (v2.6.11)
PS: No other entities in my project are using a priceFrom field.
In mappings, you define PriceFrom as integer but then you pass a decimal.
I haven't tested it but it definitely seems the major candidate as the culprit.
Francesco Abeni is right with answer. If you are already pushed something in ES as integer (or ES defined it as integer) it will generate exception when you will try to save decimal data here.
I always explicitly specify type in mapping like:
id: {"type" : "integer"}
shop_id: {"type" : "integer"}
source: {"type" : "string", "index" : "not_analyzed"}
There I see two ways to solve problem.
index alias and index merge
specify type in mapping; kill index; populate in again
I used second variant on a dev :)

Symfony child entity only being validated as Type, skipping it's own validation

I'm using the Symfony Validator on it's own, without the forms component.
I have an entity which contains a child entity, currently I can validate that that field is an instance of the child entity, but I need it to also validate the child for it's constraints.
#validation.yml
# This is the entity I'm validating against, it checks the type but doesn't then validate
# it against the child entity below.
Greg\PropertyBundle\Entity\Property:
properties:
property_id:
- NotBlank: ~
- Type:
type: string
addresses:
- All:
- Type:
type: Greg\PropertyBundle\Entity\Address
# child entity
Greg\PropertyBundle\Entity\Address:
properties:
city:
- NotBlank: ~
- Type:
type: string
To call the validator I'm passing it in with DI to one of my services and doing this:
// Validate the data
$errorList = $this->validator->validate($data);
I have also tried it by passing in the following flags:
$errorList = $this->validator->validate($data, null, true, true);
By default validation is not delegated for objects in properties. If you want to invoke validation
process for children objects then you should use specific constraint "Valid".
So your validation script will be:
#validation.yml
# This is the entity I'm validating against, it checks the type but doesn't then validate
# it against the child entity below.
Greg\PropertyBundle\Entity\Property:
properties:
property_id:
- NotBlank: ~
- Type:
type: string
addresses:
- All:
- Type:
type: Greg\PropertyBundle\Entity\Address
# addresses is array of entities, so use "traverse" option to validate each entity in that array
- Valid: { traverse: true }
# child entity
Greg\PropertyBundle\Entity\Address:
properties:
city:
- NotBlank: ~
- Type:
type: string
More details about "Valid" constraint you can find here:
http://symfony.com/doc/current/reference/constraints/Valid.html

Symfony 2 with Doctrine 2 Single Table Inheritance

In my project based on Symfony 2 + Doctrine 2 I implement model with following approach (based on FOSUserBundle source code):
All classes which belong to model are in "Model" folder of my bundle
In "Entity" folder I have classes which extend classes from Model
In "Resources/config/doctrine/" I have files with mappings in YAML format
Important: Classes for persistance (Entities) for which I want STI extend respective classes from Model, not from Entity.
The problem:
#Resources/config/doctrine/ContentElement.orm.yml
My\CoreBundle\Entity\ContentElement:
type: entity
table: content_element
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: discr
type: string
length: 255
discriminatorMap:
contentElement: ContentElementList
htmlContentElement: HtmlContentElement
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
anchor_id:
type: string
anchor_text:
type: string
#Resources/config/doctrine/HtmlContentElement.orm.yml
My\CoreBundle\Entity\HtmlContentElement:
type: entity
fields:
html:
type: text
When I try to update database I've got errors from YAML driver until I specify additionally 'id' (which should be inherited as I thought)
After adding mapping for id I have sql queries in which I see 2 separate tables for each entity.
I suspect that this happens because HtmlContentElement extends Model\HtmlContentElement but not Entity\ContentElement.
Am I right and is there known solution to my problem?

Resources