Skip to content

[Elastica] Error The message data misses id when remove in PostFlush #913

Closed
php-enqueue/enqueue-elastica-bundle
#22
@aumel

Description

@aumel

The problem is related to my last accepted PR php-enqueue/enqueue-elastica-bundle#21

The sendUpdateIndexMessage is now invoked in PostFlush. Unfortunately, the $object = $args->getObject(); has an ID null in PostFlush in a remove context.

IMO, a good solution would be to handle only ID in method sendUpdateIndexMessage and implement new method extractId.

I am working on new PR to fix this.

...
public function preRemove(LifecycleEventArgs $args)
    {
        if ($args->getObject() instanceof $this->modelClass) {
            $this->scheduledForUpdateIndex[] = [
                'action' => SyncProcessor::REMOVE_ACTION,
                'id' => $this->extractId($args->getObject())
            ];
        }
    }
...
    /**
     * @param string $action
     * @param $id
     */
    private function sendUpdateIndexMessage($action, $id)
    {
        $queue = $this->context->createQueue(Commands::SYNC_INDEX_WITH_OBJECT_CHANGE);

        $message = $this->context->createMessage(JSON::encode([
            'action' => $action,
            'model_class' => $this->modelClass,
            'model_id' => $this->config['model_id'],
            'id' => $id,
            'index_name' => $this->config['index_name'],
            'type_name' => $this->config['type_name'],
            'repository_method' => $this->config['repository_method'],
        ]));

        $this->context->createProducer()->send($queue, $message);
    }

    /**
     * @param $object
     * @return mixed
     */
    private function extractId($object)
    {
        $rp = new \ReflectionProperty($object, $this->config['model_id']);
        $rp->setAccessible(true);
        $id = $rp->getValue($object);
        $rp->setAccessible(false);

        return $id;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions