Question

I am very new to Solr and I am probably missing something simple however, having followed Xavier Briand's presentation I have set up Symfony2, Solarium and Nelmio\SolariumBundle.

"nelmio/solarium-bundle": "2.0.*@dev",
"solarium/solarium": "3.0.*"

Having implemented a toSolrDocument method for my doctrine php object.

public function toSolrDocument(\Solarium_Document_ReadWrite $doc)
{
    $doc->id          = $this->getId();
    $doc->description = $this->getTitle();
    $doc->path        = "path";

    return $doc;
}

I am faced with the following error.

Catchable Fatal Error: Argument 1 passed to ::toSolrDocument() must be an instance of Solarium_Document_ReadWrite, instance of Solarium\QueryType\Update\Query\Document given, called in Controller.php

The controller calling this toSolrDocument method has the following function

public function indexItems(){

    $client = $this->get('solarium.client');

    // get an update query instance
    $update = $client->createUpdate();

    // create documents
    $documents = array();

    $em = $this->getDoctrine()->getManager();
    $repo = $em->getRepository('<Bundle>:<Object>');
    $items = $repo->findAll();


    foreach ($items as $item) {
        $documents[] = $item->toSolrDocument($update->createDocument());
    }

    // add the documents and a commit command to the update query
    $update->addDocuments($documents);
    $update->addCommit();

    // this executes the query and returns the result
    $result = $client->update($update);
}

Most of this method again comes directly from Xavier's presentation and it is clear to see that the method $update->createDocument() does not return the correct type. In fact it returns an instance of the my php object. Does anyone know where I am going wrong here? Another thing that might be of help is that even when I try to pass a Solarium Document directly I get an exception.

 foreach ($items as $item) {
        $rw_item = new \Solarium_Document_ReadWrite();
        $documents[] = $item->toSolrDocument($rw_item);
    }

The exception is

FatalErrorException: Error: Class 'Solarium_Document_ReadWrite' not found in

I can't seem to find this class in any of the bundles and I am wondering if my setup might be causing the issues. Any help would be very much appreciated.

One additional point to note is that when I am running the solr jar I see the query requests come in from my symfony2 page it is only this indexing action that I can not work out so the config may be alright and I am miss understanding the use of the bundle.

Was it helpful?

Solution

You just need to use the correct class for the argument.

use Solarium\QueryType\Select\Result\AbstractDocument;

...

public function toSolrDocument(AbstractDocument $doc)
{

You could also not type hint it:

public function toSolrDocument($doc)
{
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top