Download the PHP package b0rner/yii2-solr without Composer
On this page you can find all versions of the php package b0rner/yii2-solr. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download b0rner/yii2-solr
More information about b0rner/yii2-solr
Files in b0rner/yii2-solr
Package yii2-solr
Short Description Solr plugin for the Yii2 framework built on top of Solarium 6 and PHP 8
License BSD-3-Clause
Informations about the package yii2-solr
yii2-solr Extension
A Yii2 Solr Extension built on top of Solarium 6 and PHP 8. This extension is based of the yii2-solr from Sammaye (https://github.com/Sammaye/yii2-solr), which unfortunately is not being developed further. Thanks Sammaye for the important work so far that has made it possible to use Solr elegantly in Yii2.
Unlike Sammaye's original extension, this version now provides ActiveRecord implementation for Solr data. This brings Solr document handling in line with Yii2's standard database ActiveRecord patterns, making it significantly easier and more intuitive to work with Solr. The ActiveRecord implementation provides familiar Yii2 patterns (find(), findOne(), save(), delete(), etc.), eliminates boilerplate code, and includes full ActiveDataProvider and ActiveFixture support.
This code is provided as is, and no guarantee is given, also no warranty/guarantee, that this code will preform in the desired way.There will be no guarantee that there will be patches for this software in the future.
This extension tries to mimic Yii's database bahavior as close as possible. As Yii provides different ways to query SQL databases, this extension does the same with SOLR. Nevertheless SQL databases and SOLR are different in many ways - so there have to be compromises.
By no means this extension covers all possibilities of SOLR nor of Solarium.
Installation
Composer ToDo
Setup
First one has to add the extension to the Yii2 configuration under components:
The options part of the configuration is a one-to-one match to Solariums own constructor and options. See documentation
Working with Query Class
A simple way to query the SOLR index is the Query Class. It provides an easy way to get documents out of an index:
Working with a connection
This next method is one of the least useful possibilities querying a SOLR index, but because it can be done it has to be mentioned:
Working with Active Record
To work with Active Record you have to create a Model, for example app/models/News.php:
There are at least two class methods that have to be implemented - attributes() and primaryKey().
The first has to be implemented in all Yii models and returns an array with all the names of needed Class proprties (aka columns in
SQL world or fields in SOLR universe):
In SOLR configuration one field will be defined as unique, storing an identifier. It is provided under <uniqueId>.
The name of this field must be provided by a Class method called primaryKey():
Having those two functions in place, you are ready to go.
Example 1
findOne() and findAll() can be used in 3 different ways:
Example 2
Working with Active Query.
When using a Model that extends from ActiveRecord, using find() returns an ActiveQuery instance. That is true while working with Yii2's own ORM as well as working
with this extension and SOLR
That said and because ActiveQuery just extends the Query Class itself you can use every b0rner\solr\Query method for finding, sorting, limiting documents in SOLR:
Note: When omitting the all() or one() methods, $result can be feed into an ActiveDataProvider instance (see below).
Insert, Update, Save, Delete
Basic CRUD operations are implemented, but this is not a relational database. Usually you've got some external process to feed documents into your SOLR index. So you might find yourself not needing anything of this.
Deleting
There are two ways:
First, using createCommand
Second, using the ActiveRecord instance
Insert, Update, Save
All three methods are actually the same thing, doing exactly the same, are even the same single method! As long, as a document has an in SOLR existing uniqueKey value, that specific document will be updated. If a provided uniqueKey is not existing in the SOLR index, a new document will be added to the index. If no uniqueKey is provided, a new document will be added as well but with a SOLR generated uniqueKey!
CAUTION: It is likely that you implement something in afterFind(), like converting a UTC datefield to some
local readable time string.
Thats why, when doing something like this, stuff can fall apart:
Using findOne() triggers afterFind(). Because SOLR stores dates in UTC, you will usually transform a date
to something readable:
Now $news->date has a different value or format than stored in SOLR. When now calling save() SOLR will throw an exception because
of the wrong timeformat.
You have to handle such things in beforeSave() or otherwise.
Highlighting
One common feature is highlighting of terms. So you can do something like this:
The term berlin - if found in fields text and/or title - will be highlighted. Those highlighted
snippets are accessable through $model->getHighlights(). It returns an array with
This can be used in afterFind() like this:
NOTE: Using hl() to activate highlighting is not necessary because using one of the hl functions implicitly
activates it.
DisMax and EDisMax Queryhandler
When triggered, this extension just uses the EDisMax Query handler. This is because everything the DisMax Handler provides is in the EDisMax handler as well. So the later is used.
You may activate the EDisMax Handler like that:
Using EDisMax with QueryFields for boostig field title:
The following functions regarding boosting and EDisMax are implemented:
bf()bq()boost()qf()for boosting specific fields by default
ActiveDataProvider
This extension provides an ActiveDataProvider:
Grouping
Solr grouping feature is provided by this extension. Useing the News class example from above (class News extends ActiveRecord):
Last
To get a list of all suppoerte Solr parameters see Query-php
Credits
Co-authored-by: rmatulat https://github.com/rmatulat Special thanks to @rmatulat for contributing the majority of ActiveRecord, ActiveDataProvider, ActiveFixtures.