sammaye/yii2-solr [solved]

I am trying to implement Sammaye’s Yii2 extension for Solarium/Solr (

Currently I am able to query solr and get results using:

# models/ProfileSearch.php:

$query = Yii::$app->solr->createSelect();

$query->setQuery($term . '&wt=php');


$this->result = Yii::$app->solr->select($query);

But I run into a problem when I try to feed the query to a dataProvider and then a widget. According to the extension instructions, I do this:

# models/ProfileSearch.php

use sammaye\solr\SolrResult;

    public function query($term)


        $query = Yii::$app->solr->createSelect();

        $query->setQuery($term . '&wt=php');


        $this->result = Yii::$app->solr->select($query);

        $dataProvider = new SolrDataProvider([

            'query' => $query,

            'modelClass' => 'SolrResult',   <<<==========

            'pagination' => ['pageSize'=>10],


        return $dataProvider;


# views/profile/search.php

    echo yii\widgets\ListView::widget([

        'dataProvider' => $dataProvider,    <<<==========

        'showOnEmpty' => false,

        'emptyText' => '',

        'itemView' => '_view',

        'itemOptions' => ['class' => 'item-bordered'],

        'layout' => '<div class="summary-row hidden-print clearfix">{summary}<ul class="list-sorter"><li><b>Sort By:</b></li><li>{sorter}</li></ul></div>{items}{pager}',


But this throws an error: “Class ‘SolrResult’ not found”. The SolrDataProvider class (vendor/sammaye/yii2-solr/SolrDataProvider.php) makes a call


but cannot find the SolrResult modelClass and I’m not sure why. Does this have something to do with the namespace? Where else would it be looking for the class?

SolrResult looks like this:

# vendor/sammaye/yii2-solr/SolrResult.php


namespace sammaye\solr;

class SolrResult extends Model implements SolrDocumentInterface


    public static function populateFromSolr($doc)


        if($doc->document_type === 'profile'){

            $model = new Profile();

            $model->id = Solr::int($doc->id);


        return $model;



I have also tried putting the file in app/models, but that doesn’t work either. Any thoughts as to why the modelClass cannot be found? According to the extension author, this is similar to the way a modelClass was passed to a dataProvider in Yii1, but I’m fairly new to Yii, so I’m not sure if this is a problem with dataproviders in general or with this extension in particular.

Change modelClass to "smmaye\solr\SolrResult" and it will work. This is since being a string Yi2 is including it from a different scope whereby it is not known you included it in the view.

Magic. I’m off and running…almost.

I have two follow up questions.

  1. What class should sorlResult be extending? I’m assuming my Yii class which corresponds to the db table that solr is indexing from, in my case “Profile”.

  2. solrResult is being passed a single $doc, but there is no document_type, so it never enters the loop to return an object. I’m not sure what document_type is. I take it to be the entity name defined in solr-config?

# config/dataconfig.xml


    <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/ibn" user="XXXXX" password="XXXX" />


        <entity name="profile" query="select * from profile" >

        	<field column="id" name="id" />




My solr query response from the solr console looks like this:












        'phone'=>'(555) 555-5555',





$doc looks like this:

object(Solarium\QueryType\Select\Result\Document)#98 (1) { ["fields":protected]=> array(21) { ["phone"]=> string(14) "(555) 555-5555"




 [ ["_version_"]=> int(1528021068318507008) ["score"]=> float(0.22609535) } } 

SolrResult can work on it’s own. It is great like that. It is just a container to apply properties to itself or another object.

Sometimes you don’t have a document type, frankly that is something I do for my personal logic. You can instead iterate doc in response

Great. It works if I just iterate through $this->result = Yii::$app->solr->select($query);

I’ll have to spend a little time tweeking it to get the results I want with the widget. Thanks much for your help. I’m off to the races…

It looks like, the SolrResult.php is currently not part of the extension. Right?

So who would look like the implementation / example of using the SolrDataProvider?

An 2nd Question: I have some queries, that returns exactly one document. Is there a way to access exact this one document without iteration trough $result (coming from $result=Yii::$app->solr->select($query);)?

Thank you