cob cob
cob cob

Reputation: 11

Symfony2 :FOS:elastica populate fails for serializer with callback

I am using symfony LTS with master FOS:elastica and master jms_serializer.

Config

fos_elastica:
clients:
    default: { host: %elastic_host%, port: %elastic_port% }
serializer:
    callback_class: FOS\ElasticaBundle\Serializer\Callback
    serializer: serializer
indexes:
    c_search:
        client: default
        types:
            adverts:
                mappings:
                    id:
                        type: integer
                    user:
                        type: string
                    Media:
                        type: nested
                        properties:
                            urlMedia: ~
                persistence:
                    driver: orm
                    model: WebSite\MyBundle\Entity\Advert
                    provider: ~
                    listener: ~
                    finder: ~

Populate

php app/console fos:elastica:populate

This command work without any problem before I added the lines below. When I try to run "php app/console fos:elastica:populate" there is an infinite loop since I have added these lines:

serializer:
    callback_class: FOS\ElasticaBundle\Serializer\Callback
    serializer: serializer 

Troubleshooting:

After this I made a simple request (without serialize because of my problem) like this on my controller :

    public function testAction()
    {
        $repositoryManager = $this->container->get('fos_elastica.manager');
        $repository = $repositoryManager->getRepository('WebSiteMyBundle:Advert');
        $data = json_encode($repository->find('68200'));
        return $this->render('WebSiteMyBundle:Default:test.html.twig', array(
            'test'=> $data,
        ));
    }

On my result test there is like 5 empty arrays. I know the result is good since there is normaly 5 response in my raw request but I can't find the solution to show the real content if anyone have an idea.

Upvotes: 0

Views: 2576

Answers (1)

lenybernard
lenybernard

Reputation: 2448

Ok, I was right in my comment, this issue was related to serializer and is quite logical. Actually, if you do not specify anything, elastic will index every property and every relation in cascade which is a problem when having some circular references.

Here is what you could do to fix you problem :

First, add some properties in your group :

<?php

namespace WebSite\MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;

/**
 * Advert
 *
 * @ORM\Table(name="advert")
 */
class Advert
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=55)
     * @Serializer\Groups({"elastica"})
     */
    private $title;

    ...

Then, add this in your config file :

fos_elastica:
    clients:
        default: { host: %elastic_host%, port: %elastic_port% }
    serializer:
        callback_class: FOS\ElasticaBundle\Serializer\Callback
        serializer: serializer
    indexes:
        c_search:
            client: default
            types:
                adverts:
                    serializer:
                        groups: [elastica]
                    mappings:
                        id:
                            type: integer
                        user:
                            type: string
                        Media:
                            type: nested
                            properties:
                                urlMedia: ~
                    persistence:
                        driver: orm
                        model: WebSite\MyBundle\Entity\Advert
                        provider: ~
                        listener: ~
                        finder: ~

Then, you shouldn't have any circular reference and only having title property in your elasticsearch index.

Good luck.

Upvotes: 1

Related Questions