I am trying to implement Sammaye’s Yii2 extension for Solarium/Solr (https://github.com/Sammaye/yii2-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');
$query->getEDisMax();
$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');
$query->getEDisMax();
$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
$this->modelClass
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
<?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.